WhatsNewActivity.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.content.Context;
  24. import android.content.Intent;
  25. import android.os.Build;
  26. import android.os.Bundle;
  27. import android.view.View;
  28. import android.widget.Button;
  29. import android.widget.ImageButton;
  30. import android.widget.TextView;
  31. import com.nextcloud.client.di.Injectable;
  32. import com.nextcloud.client.preferences.AppPreferences;
  33. import com.owncloud.android.MainApp;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.authentication.AccountUtils;
  36. import com.owncloud.android.features.FeatureItem;
  37. import com.owncloud.android.ui.adapter.FeaturesViewAdapter;
  38. import com.owncloud.android.ui.adapter.FeaturesWebViewAdapter;
  39. import com.owncloud.android.ui.whatsnew.ProgressIndicator;
  40. import com.owncloud.android.utils.ThemeUtils;
  41. import javax.inject.Inject;
  42. import androidx.fragment.app.FragmentActivity;
  43. import androidx.viewpager.widget.ViewPager;
  44. /**
  45. * Activity displaying new features after an update.
  46. */
  47. public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, Injectable {
  48. private ImageButton mForwardFinishButton;
  49. private Button mSkipButton;
  50. private ProgressIndicator mProgress;
  51. private ViewPager mPager;
  52. @Inject AppPreferences preferences;
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.whats_new_activity);
  57. int fontColor = getResources().getColor(R.color.login_text_color);
  58. mProgress = findViewById(R.id.progressIndicator);
  59. mPager = findViewById(R.id.contentPanel);
  60. String[] urls = getResources().getStringArray(R.array.whatsnew_urls);
  61. boolean showWebView = urls.length > 0;
  62. if (showWebView) {
  63. FeaturesWebViewAdapter featuresWebViewAdapter = new FeaturesWebViewAdapter(getSupportFragmentManager(),
  64. urls);
  65. mProgress.setNumberOfSteps(featuresWebViewAdapter.getCount());
  66. mPager.setAdapter(featuresWebViewAdapter);
  67. } else {
  68. FeaturesViewAdapter featuresViewAdapter = new FeaturesViewAdapter(getSupportFragmentManager(),
  69. getWhatsNew(this, preferences));
  70. mProgress.setNumberOfSteps(featuresViewAdapter.getCount());
  71. mPager.setAdapter(featuresViewAdapter);
  72. }
  73. mPager.addOnPageChangeListener(this);
  74. mForwardFinishButton = findViewById(R.id.forward);
  75. ThemeUtils.colorImageButton(mForwardFinishButton, fontColor);
  76. mForwardFinishButton.setOnClickListener(view -> {
  77. if (mProgress.hasNextStep()) {
  78. mPager.setCurrentItem(mPager.getCurrentItem() + 1, true);
  79. mProgress.animateToStep(mPager.getCurrentItem() + 1);
  80. } else {
  81. onFinish();
  82. finish();
  83. }
  84. updateNextButtonIfNeeded();
  85. });
  86. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  87. mForwardFinishButton.setBackground(null);
  88. } else {
  89. mForwardFinishButton.setBackgroundDrawable(null);
  90. }
  91. mSkipButton = findViewById(R.id.skip);
  92. mSkipButton.setTextColor(fontColor);
  93. mSkipButton.setOnClickListener(view -> {
  94. onFinish();
  95. finish();
  96. });
  97. TextView tv = findViewById(R.id.welcomeText);
  98. if (showWebView) {
  99. tv.setText(R.string.app_name);
  100. } else {
  101. tv.setText(String.format(getString(R.string.whats_new_title), MainApp.getVersionName()));
  102. }
  103. updateNextButtonIfNeeded();
  104. }
  105. @Override
  106. public void onBackPressed() {
  107. onFinish();
  108. super.onBackPressed();
  109. }
  110. private void updateNextButtonIfNeeded() {
  111. if (!mProgress.hasNextStep()) {
  112. mForwardFinishButton.setImageResource(R.drawable.ic_ok);
  113. mSkipButton.setVisibility(View.INVISIBLE);
  114. } else {
  115. mForwardFinishButton.setImageResource(R.drawable.arrow_right);
  116. mSkipButton.setVisibility(View.VISIBLE);
  117. }
  118. }
  119. private void onFinish() {
  120. preferences.setLastSeenVersionCode(MainApp.getVersionCode());
  121. }
  122. public static void runIfNeeded(Context context, AppPreferences preferences) {
  123. if (!context.getResources().getBoolean(R.bool.show_whats_new) || context instanceof WhatsNewActivity) {
  124. return;
  125. }
  126. if (shouldShow(context, preferences)) {
  127. context.startActivity(new Intent(context, WhatsNewActivity.class));
  128. }
  129. }
  130. private static boolean shouldShow(Context context, AppPreferences preferences) {
  131. return !(context instanceof PassCodeActivity) && getWhatsNew(context, preferences).length > 0;
  132. }
  133. @Override
  134. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  135. // unused but to be implemented due to abstract parent
  136. }
  137. @Override
  138. public void onPageSelected(int position) {
  139. mProgress.animateToStep(position + 1);
  140. updateNextButtonIfNeeded();
  141. }
  142. @Override
  143. public void onPageScrollStateChanged(int state) {
  144. // unused but to be implemented due to abstract parent
  145. }
  146. static private boolean isFirstRun(Context context) {
  147. return AccountUtils.getCurrentOwnCloudAccount(context) == null;
  148. }
  149. private static FeatureItem[] getWhatsNew(Context context, AppPreferences preferences) {
  150. int itemVersionCode = 30030099;
  151. if (!isFirstRun(context) && MainApp.getVersionCode() >= itemVersionCode
  152. && preferences.getLastSeenVersionCode() < itemVersionCode) {
  153. return new FeatureItem[]{new FeatureItem(R.drawable.whats_new_device_credentials,
  154. R.string.whats_new_device_credentials_title, R.string.whats_new_device_credentials_content,
  155. false, false)};
  156. } else {
  157. return new FeatureItem[0];
  158. }
  159. }
  160. }