FirstRunActivity.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.net.Uri;
  30. import android.os.Bundle;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.Button;
  34. import android.widget.LinearLayout;
  35. import android.widget.TextView;
  36. import com.nextcloud.client.account.UserAccountManager;
  37. import com.nextcloud.client.appinfo.AppInfo;
  38. import com.nextcloud.client.di.Injectable;
  39. import com.nextcloud.client.preferences.AppPreferences;
  40. import com.owncloud.android.BuildConfig;
  41. import com.owncloud.android.R;
  42. import com.owncloud.android.authentication.AuthenticatorActivity;
  43. import com.owncloud.android.features.FeatureItem;
  44. import com.owncloud.android.ui.activity.BaseActivity;
  45. import com.owncloud.android.ui.activity.FileDisplayActivity;
  46. import com.owncloud.android.ui.adapter.FeaturesViewAdapter;
  47. import com.owncloud.android.ui.whatsnew.ProgressIndicator;
  48. import com.owncloud.android.utils.DisplayUtils;
  49. import javax.inject.Inject;
  50. import androidx.viewpager.widget.ViewPager;
  51. /**
  52. * Activity displaying general feature after a fresh install.
  53. */
  54. public class FirstRunActivity extends BaseActivity implements ViewPager.OnPageChangeListener, Injectable {
  55. public static final String EXTRA_ALLOW_CLOSE = "ALLOW_CLOSE";
  56. public static final String EXTRA_EXIT = "EXIT";
  57. public static final int FIRST_RUN_RESULT_CODE = 199;
  58. private ProgressIndicator progressIndicator;
  59. @Inject UserAccountManager userAccountManager;
  60. @Inject AppPreferences preferences;
  61. @Inject AppInfo appInfo;
  62. @Inject OnboardingService onboarding;
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.first_run_activity);
  67. boolean isProviderOrOwnInstallationVisible = getResources().getBoolean(R.bool.show_provider_or_own_installation);
  68. setSlideshowSize(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
  69. Button loginButton = findViewById(R.id.login);
  70. loginButton.setBackgroundColor(getResources().getColor(R.color.bg_default));
  71. loginButton.setTextColor(getResources().getColor(R.color.primary));
  72. loginButton.setOnClickListener(v -> {
  73. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  74. Intent authenticatorActivityIntent = new Intent(this, AuthenticatorActivity.class);
  75. authenticatorActivityIntent.putExtra(AuthenticatorActivity.EXTRA_USE_PROVIDER_AS_WEBLOGIN, false);
  76. startActivityForResult(authenticatorActivityIntent, FIRST_RUN_RESULT_CODE);
  77. } else {
  78. finish();
  79. }
  80. });
  81. Button providerButton = findViewById(R.id.signup);
  82. providerButton.setBackgroundColor(getResources().getColor(R.color.primary));
  83. providerButton.setTextColor(getResources().getColor(R.color.login_text_color));
  84. providerButton.setVisibility(isProviderOrOwnInstallationVisible ? View.VISIBLE : View.GONE);
  85. providerButton.setOnClickListener(v -> {
  86. Intent authenticatorActivityIntent = new Intent(this, AuthenticatorActivity.class);
  87. authenticatorActivityIntent.putExtra(AuthenticatorActivity.EXTRA_USE_PROVIDER_AS_WEBLOGIN, true);
  88. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  89. startActivityForResult(authenticatorActivityIntent, FIRST_RUN_RESULT_CODE);
  90. } else {
  91. authenticatorActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  92. startActivity(authenticatorActivityIntent);
  93. }
  94. });
  95. TextView hostOwnServerTextView = findViewById(R.id.host_own_server);
  96. hostOwnServerTextView.setTextColor(getResources().getColor(R.color.login_text_color));
  97. hostOwnServerTextView.setVisibility(isProviderOrOwnInstallationVisible ? View.VISIBLE : View.GONE);
  98. progressIndicator = findViewById(R.id.progressIndicator);
  99. ViewPager viewPager = findViewById(R.id.contentPanel);
  100. // Sometimes, accounts are not deleted when you uninstall the application so we'll do it now
  101. if (onboarding.isFirstRun()) {
  102. userAccountManager.removeAllAccounts();
  103. }
  104. FeaturesViewAdapter featuresViewAdapter = new FeaturesViewAdapter(getSupportFragmentManager(), getFirstRun());
  105. progressIndicator.setNumberOfSteps(featuresViewAdapter.getCount());
  106. viewPager.setAdapter(featuresViewAdapter);
  107. viewPager.addOnPageChangeListener(this);
  108. }
  109. private void setSlideshowSize(boolean isLandscape) {
  110. boolean isProviderOrOwnInstallationVisible = getResources().getBoolean(R.bool.show_provider_or_own_installation);
  111. LinearLayout buttonLayout = findViewById(R.id.buttonLayout);
  112. LinearLayout.LayoutParams layoutParams;
  113. buttonLayout.setOrientation(isLandscape ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
  114. LinearLayout bottomLayout = findViewById(R.id.bottomLayout);
  115. if (isProviderOrOwnInstallationVisible) {
  116. layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  117. ViewGroup.LayoutParams.WRAP_CONTENT);
  118. } else {
  119. layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  120. DisplayUtils.convertDpToPixel(isLandscape ? 100f : 150f, this));
  121. }
  122. bottomLayout.setLayoutParams(layoutParams);
  123. }
  124. @Override
  125. public void onConfigurationChanged(Configuration newConfig) {
  126. super.onConfigurationChanged(newConfig);
  127. if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  128. setSlideshowSize(true);
  129. } else {
  130. setSlideshowSize(false);
  131. }
  132. }
  133. @Override
  134. public void onBackPressed() {
  135. onFinish();
  136. if (getIntent().getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
  137. super.onBackPressed();
  138. } else {
  139. Intent intent = new Intent(getApplicationContext(), AuthenticatorActivity.class);
  140. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  141. intent.putExtra(EXTRA_EXIT, true);
  142. startActivity(intent);
  143. finish();
  144. }
  145. }
  146. private void onFinish() {
  147. preferences.setLastSeenVersionCode(BuildConfig.VERSION_CODE);
  148. }
  149. @Override
  150. protected void onStop() {
  151. onFinish();
  152. super.onStop();
  153. }
  154. @Override
  155. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  156. // unused but to be implemented due to abstract parent
  157. }
  158. @Override
  159. public void onPageSelected(int position) {
  160. progressIndicator.animateToStep(position + 1);
  161. }
  162. @Override
  163. public void onPageScrollStateChanged(int state) {
  164. // unused but to be implemented due to abstract parent
  165. }
  166. public void onHostYourOwnServerClick(View view) {
  167. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_server_install)));
  168. DisplayUtils.startIntentIfAppAvailable(intent, this, R.string.no_browser_available);
  169. }
  170. @Override
  171. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  172. super.onActivityResult(requestCode, resultCode, data);
  173. if (FIRST_RUN_RESULT_CODE == requestCode && RESULT_OK == resultCode) {
  174. String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
  175. Account account = userAccountManager.getAccountByName(accountName);
  176. if (account == null) {
  177. DisplayUtils.showSnackMessage(this, R.string.account_creation_failed);
  178. return;
  179. }
  180. setAccount(account);
  181. userAccountManager.setCurrentOwnCloudAccount(account.name);
  182. onAccountSet();
  183. Intent i = new Intent(this, FileDisplayActivity.class);
  184. i.setAction(FileDisplayActivity.RESTART);
  185. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  186. startActivity(i);
  187. }
  188. }
  189. public static FeatureItem[] getFirstRun() {
  190. return new FeatureItem[]{
  191. new FeatureItem(R.drawable.logo, R.string.first_run_1_text, R.string.empty, true, false),
  192. new FeatureItem(R.drawable.first_run_files, R.string.first_run_2_text, R.string.empty, true, false),
  193. new FeatureItem(R.drawable.first_run_groupware, R.string.first_run_3_text, R.string.empty, true, false),
  194. new FeatureItem(R.drawable.first_run_talk, R.string.first_run_4_text, R.string.empty, true, false)};
  195. }
  196. }