FirstRunActivity.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. private static boolean isFirstRun(Context context) {
  140. return AccountUtils.getCurrentOwnCloudAccount(context) == null;
  141. }
  142. public static boolean runIfNeeded(Context context) {
  143. if (context instanceof FirstRunActivity) {
  144. return false;
  145. }
  146. if (isFirstRun(context) && context instanceof AuthenticatorActivity) {
  147. context.startActivity(new Intent(context, FirstRunActivity.class));
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. @Override
  154. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  155. // unused but to be implemented due to abstract parent
  156. }
  157. @Override
  158. public void onPageSelected(int position) {
  159. progressIndicator.animateToStep(position + 1);
  160. }
  161. @Override
  162. public void onPageScrollStateChanged(int state) {
  163. // unused but to be implemented due to abstract parent
  164. }
  165. public void onHostYourOwnServerClick(View view) {
  166. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_server_install))));
  167. }
  168. @Override
  169. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  170. if (FIRST_RUN_RESULT_CODE == requestCode && RESULT_OK == resultCode) {
  171. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  172. onAccountSet(false);
  173. Intent i = new Intent(this, FileDisplayActivity.class);
  174. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  175. startActivity(i);
  176. }
  177. }
  178. public static FeatureItem[] getFirstRun() {
  179. return new FeatureItem[]{
  180. new FeatureItem(R.drawable.logo, R.string.first_run_1_text, R.string.empty, true, false),
  181. new FeatureItem(R.drawable.first_run_files, R.string.first_run_2_text, R.string.empty, true, false),
  182. new FeatureItem(R.drawable.first_run_groupware, R.string.first_run_3_text, R.string.empty, true, false),
  183. new FeatureItem(R.drawable.first_run_talk, R.string.first_run_4_text, R.string.empty, true, false)};
  184. }
  185. }