FirstRunActivity.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Bartosz Przybylski
  5. * @author Chris Narkiewicz
  6. * Copyright (C) 2015 Bartosz Przybylski
  7. * Copyright (C) 2015 ownCloud Inc.
  8. * Copyright (C) 2016 Nextcloud.
  9. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public
  22. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.nextcloud.client.onboarding;
  25. import android.accounts.Account;
  26. import android.accounts.AccountManager;
  27. import android.content.Intent;
  28. import android.content.res.Configuration;
  29. import android.graphics.Color;
  30. import android.net.Uri;
  31. import android.os.Bundle;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import android.widget.Button;
  35. import android.widget.LinearLayout;
  36. import android.widget.TextView;
  37. import com.nextcloud.client.account.UserAccountManager;
  38. import com.nextcloud.client.appinfo.AppInfo;
  39. import com.nextcloud.client.di.Injectable;
  40. import com.nextcloud.client.preferences.AppPreferences;
  41. import com.owncloud.android.BuildConfig;
  42. import com.owncloud.android.R;
  43. import com.owncloud.android.authentication.AuthenticatorActivity;
  44. import com.owncloud.android.features.FeatureItem;
  45. import com.owncloud.android.ui.activity.BaseActivity;
  46. import com.owncloud.android.ui.activity.FileDisplayActivity;
  47. import com.owncloud.android.ui.adapter.FeaturesViewAdapter;
  48. import com.owncloud.android.ui.whatsnew.ProgressIndicator;
  49. import com.owncloud.android.utils.DisplayUtils;
  50. import javax.inject.Inject;
  51. import androidx.viewpager.widget.ViewPager;
  52. /**
  53. * Activity displaying general feature after a fresh install.
  54. */
  55. public class FirstRunActivity extends BaseActivity implements ViewPager.OnPageChangeListener, Injectable {
  56. public static final String EXTRA_ALLOW_CLOSE = "ALLOW_CLOSE";
  57. public static final String EXTRA_EXIT = "EXIT";
  58. public static final int FIRST_RUN_RESULT_CODE = 199;
  59. private ProgressIndicator progressIndicator;
  60. @Inject UserAccountManager userAccountManager;
  61. @Inject AppPreferences preferences;
  62. @Inject AppInfo appInfo;
  63. @Inject OnboardingService onboarding;
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.first_run_activity);
  68. boolean isProviderOrOwnInstallationVisible = getResources().getBoolean(R.bool.show_provider_or_own_installation);
  69. setSlideshowSize(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
  70. Button loginButton = findViewById(R.id.login);
  71. loginButton.setBackgroundColor(Color.WHITE);
  72. loginButton.setTextColor(getResources().getColor(R.color.primary));
  73. loginButton.setOnClickListener(v -> {
  74. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  75. Intent authenticatorActivityIntent = new Intent(this, AuthenticatorActivity.class);
  76. authenticatorActivityIntent.putExtra(AuthenticatorActivity.EXTRA_USE_PROVIDER_AS_WEBLOGIN, false);
  77. startActivityForResult(authenticatorActivityIntent, FIRST_RUN_RESULT_CODE);
  78. } else {
  79. finish();
  80. }
  81. });
  82. Button providerButton = findViewById(R.id.signup);
  83. providerButton.setBackgroundColor(getResources().getColor(R.color.primary_dark));
  84. providerButton.setTextColor(getResources().getColor(R.color.login_text_color));
  85. providerButton.setVisibility(isProviderOrOwnInstallationVisible ? View.VISIBLE : View.GONE);
  86. providerButton.setOnClickListener(v -> {
  87. Intent authenticatorActivityIntent = new Intent(this, AuthenticatorActivity.class);
  88. authenticatorActivityIntent.putExtra(AuthenticatorActivity.EXTRA_USE_PROVIDER_AS_WEBLOGIN, true);
  89. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  90. startActivityForResult(authenticatorActivityIntent, FIRST_RUN_RESULT_CODE);
  91. } else {
  92. authenticatorActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  93. startActivity(authenticatorActivityIntent);
  94. }
  95. });
  96. TextView hostOwnServerTextView = findViewById(R.id.host_own_server);
  97. hostOwnServerTextView.setTextColor(getResources().getColor(R.color.login_text_color));
  98. hostOwnServerTextView.setVisibility(isProviderOrOwnInstallationVisible ? View.VISIBLE : View.GONE);
  99. progressIndicator = findViewById(R.id.progressIndicator);
  100. ViewPager viewPager = findViewById(R.id.contentPanel);
  101. // Sometimes, accounts are not deleted when you uninstall the application so we'll do it now
  102. if (onboarding.isFirstRun()) {
  103. userAccountManager.removeAllAccounts();
  104. }
  105. FeaturesViewAdapter featuresViewAdapter = new FeaturesViewAdapter(getSupportFragmentManager(), getFirstRun());
  106. progressIndicator.setNumberOfSteps(featuresViewAdapter.getCount());
  107. viewPager.setAdapter(featuresViewAdapter);
  108. viewPager.addOnPageChangeListener(this);
  109. }
  110. private void setSlideshowSize(boolean isLandscape) {
  111. boolean isProviderOrOwnInstallationVisible = getResources().getBoolean(R.bool.show_provider_or_own_installation);
  112. LinearLayout buttonLayout = findViewById(R.id.buttonLayout);
  113. LinearLayout.LayoutParams layoutParams;
  114. buttonLayout.setOrientation(isLandscape ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
  115. LinearLayout bottomLayout = findViewById(R.id.bottomLayout);
  116. if (isProviderOrOwnInstallationVisible) {
  117. layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  118. ViewGroup.LayoutParams.WRAP_CONTENT);
  119. } else {
  120. layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  121. DisplayUtils.convertDpToPixel(isLandscape ? 100f : 150f, this));
  122. }
  123. bottomLayout.setLayoutParams(layoutParams);
  124. }
  125. @Override
  126. public void onConfigurationChanged(Configuration newConfig) {
  127. super.onConfigurationChanged(newConfig);
  128. if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  129. setSlideshowSize(true);
  130. } else {
  131. setSlideshowSize(false);
  132. }
  133. }
  134. @Override
  135. public void onBackPressed() {
  136. onFinish();
  137. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  138. super.onBackPressed();
  139. } else {
  140. Intent intent = new Intent(getApplicationContext(), AuthenticatorActivity.class);
  141. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  142. intent.putExtra(EXTRA_EXIT, true);
  143. startActivity(intent);
  144. finish();
  145. }
  146. }
  147. private void onFinish() {
  148. preferences.setLastSeenVersionCode(BuildConfig.VERSION_CODE);
  149. }
  150. @Override
  151. protected void onStop() {
  152. onFinish();
  153. super.onStop();
  154. }
  155. @Override
  156. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  157. // unused but to be implemented due to abstract parent
  158. }
  159. @Override
  160. public void onPageSelected(int position) {
  161. progressIndicator.animateToStep(position + 1);
  162. }
  163. @Override
  164. public void onPageScrollStateChanged(int state) {
  165. // unused but to be implemented due to abstract parent
  166. }
  167. public void onHostYourOwnServerClick(View view) {
  168. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_server_install)));
  169. DisplayUtils.startIntentIfAppAvailable(intent, this, R.string.no_browser_available);
  170. }
  171. @Override
  172. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  173. super.onActivityResult(requestCode, resultCode, data);
  174. if (FIRST_RUN_RESULT_CODE == requestCode && RESULT_OK == resultCode) {
  175. String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
  176. Account account = userAccountManager.getAccountByName(accountName);
  177. if (account == null) {
  178. DisplayUtils.showSnackMessage(this, R.string.account_creation_failed);
  179. return;
  180. }
  181. setAccount(account);
  182. userAccountManager.setCurrentOwnCloudAccount(account.name);
  183. onAccountSet(false);
  184. Intent i = new Intent(this, FileDisplayActivity.class);
  185. i.setAction(FileDisplayActivity.RESTART);
  186. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  187. startActivity(i);
  188. }
  189. }
  190. public static FeatureItem[] getFirstRun() {
  191. return new FeatureItem[]{
  192. new FeatureItem(R.drawable.logo, R.string.first_run_1_text, R.string.empty, true, false),
  193. new FeatureItem(R.drawable.first_run_files, R.string.first_run_2_text, R.string.empty, true, false),
  194. new FeatureItem(R.drawable.first_run_groupware, R.string.first_run_3_text, R.string.empty, true, false),
  195. new FeatureItem(R.drawable.first_run_talk, R.string.first_run_4_text, R.string.empty, true, false)};
  196. }
  197. }