FirstRunActivity.java 9.6 KB

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