WhatsNewActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * <p>
  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. * <p>
  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. * <p>
  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.SharedPreferences;
  28. import android.os.Build;
  29. import android.os.Bundle;
  30. import android.preference.PreferenceManager;
  31. import android.support.annotation.Nullable;
  32. import android.support.v4.app.Fragment;
  33. import android.support.v4.app.FragmentActivity;
  34. import android.support.v4.app.FragmentManager;
  35. import android.support.v4.app.FragmentPagerAdapter;
  36. import android.support.v4.view.ViewPager;
  37. import android.view.Gravity;
  38. import android.view.LayoutInflater;
  39. import android.view.View;
  40. import android.view.ViewGroup;
  41. import android.webkit.WebView;
  42. import android.webkit.WebViewClient;
  43. import android.widget.Button;
  44. import android.widget.ImageButton;
  45. import android.widget.ImageView;
  46. import android.widget.TextView;
  47. import com.owncloud.android.MainApp;
  48. import com.owncloud.android.R;
  49. import com.owncloud.android.authentication.AccountAuthenticatorActivity;
  50. import com.owncloud.android.authentication.AccountUtils;
  51. import com.owncloud.android.features.FeatureList;
  52. import com.owncloud.android.features.FeatureList.FeatureItem;
  53. import com.owncloud.android.ui.whatsnew.ProgressIndicator;
  54. import com.owncloud.android.utils.AnalyticsUtils;
  55. /**
  56. * Activity displaying general feature after a fresh install and new features after an update.
  57. */
  58. public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
  59. private static final String KEY_LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
  60. private static final String SCREEN_NAME = "What's new";
  61. private static final String TAG = WhatsNewActivity.class.getSimpleName();
  62. private ImageButton mForwardFinishButton;
  63. private Button mSkipButton;
  64. private ProgressIndicator mProgress;
  65. private ViewPager mPager;
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. setContentView(R.layout.whats_new_activity);
  70. mProgress = (ProgressIndicator) findViewById(R.id.progressIndicator);
  71. mPager = (ViewPager) findViewById(R.id.contentPanel);
  72. final boolean isBeta = getResources().getBoolean(R.bool.is_beta);
  73. String[] urls = getResources().getStringArray(R.array.whatsnew_urls);
  74. // Sometimes, accounts are not deleted when you uninstall the application so we'll do it now
  75. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  76. for (Account account : AccountUtils.getAccounts(this)) {
  77. am.removeAccount(account, null, null);
  78. }
  79. boolean showWebView = urls.length > 0;
  80. if (showWebView) {
  81. FeaturesWebViewAdapter featuresWebViewAdapter = new FeaturesWebViewAdapter(getSupportFragmentManager(),
  82. urls);
  83. mProgress.setNumberOfSteps(featuresWebViewAdapter.getCount());
  84. mPager.setAdapter(featuresWebViewAdapter);
  85. } else {
  86. FeaturesViewAdapter featuresViewAdapter = new FeaturesViewAdapter(getSupportFragmentManager(),
  87. FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta));
  88. mProgress.setNumberOfSteps(featuresViewAdapter.getCount());
  89. mPager.setAdapter(featuresViewAdapter);
  90. }
  91. mPager.addOnPageChangeListener(this);
  92. mForwardFinishButton = (ImageButton) findViewById(R.id.forward);
  93. mForwardFinishButton.setOnClickListener(new View.OnClickListener() {
  94. @Override
  95. public void onClick(View view) {
  96. if (mProgress.hasNextStep()) {
  97. mPager.setCurrentItem(mPager.getCurrentItem() + 1, true);
  98. mProgress.animateToStep(mPager.getCurrentItem() + 1);
  99. } else {
  100. onFinish();
  101. finish();
  102. }
  103. updateNextButtonIfNeeded();
  104. }
  105. });
  106. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  107. mForwardFinishButton.setBackground(null);
  108. } else {
  109. mForwardFinishButton.setBackgroundDrawable(null);
  110. }
  111. mSkipButton = (Button) findViewById(R.id.skip);
  112. mSkipButton.setOnClickListener(new View.OnClickListener() {
  113. @Override
  114. public void onClick(View view) {
  115. onFinish();
  116. finish();
  117. }
  118. });
  119. TextView tv = (TextView) findViewById(R.id.welcomeText);
  120. if (showWebView) {
  121. tv.setText(R.string.app_name);
  122. } else if (isFirstRun()) {
  123. tv.setText(R.string.empty);
  124. } else {
  125. tv.setText(String.format(getString(R.string.whats_new_title), MainApp.getVersionName()));
  126. }
  127. updateNextButtonIfNeeded();
  128. }
  129. @Override
  130. protected void onResume() {
  131. super.onResume();
  132. AnalyticsUtils.setCurrentScreenName(this, SCREEN_NAME, TAG);
  133. }
  134. @Override
  135. public void onBackPressed() {
  136. onFinish();
  137. super.onBackPressed();
  138. }
  139. private void updateNextButtonIfNeeded() {
  140. if (!mProgress.hasNextStep()) {
  141. mForwardFinishButton.setImageResource(R.drawable.ic_done_white);
  142. mSkipButton.setVisibility(View.INVISIBLE);
  143. } else {
  144. mForwardFinishButton.setImageResource(R.drawable.arrow_right);
  145. mSkipButton.setVisibility(View.VISIBLE);
  146. }
  147. }
  148. private void onFinish() {
  149. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  150. SharedPreferences.Editor editor = pref.edit();
  151. editor.putInt(KEY_LAST_SEEN_VERSION_CODE, MainApp.getVersionCode());
  152. editor.apply();
  153. }
  154. static private int getLastSeenVersionCode() {
  155. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
  156. return pref.getInt(KEY_LAST_SEEN_VERSION_CODE, 0);
  157. }
  158. static private boolean isFirstRun() {
  159. return getLastSeenVersionCode() == 0 && AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext()) == null;
  160. }
  161. static public void runIfNeeded(Context context) {
  162. if (!context.getResources().getBoolean(R.bool.show_whats_new)) {
  163. return;
  164. }
  165. if (context instanceof WhatsNewActivity) {
  166. return;
  167. }
  168. if (shouldShow(context)) {
  169. context.startActivity(new Intent(context, WhatsNewActivity.class));
  170. }
  171. }
  172. static private boolean shouldShow(Context context) {
  173. final boolean isBeta = context.getResources().getBoolean(R.bool.is_beta);
  174. return (isFirstRun() && context instanceof AccountAuthenticatorActivity) ||
  175. (
  176. !(isFirstRun() && (context instanceof FileDisplayActivity)) &&
  177. !(context instanceof PassCodeActivity) &&
  178. (FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta).length > 0)
  179. );
  180. }
  181. @Override
  182. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  183. // unused but to be implemented due to abstract parent
  184. }
  185. @Override
  186. public void onPageSelected(int position) {
  187. mProgress.animateToStep(position + 1);
  188. updateNextButtonIfNeeded();
  189. }
  190. @Override
  191. public void onPageScrollStateChanged(int state) {
  192. // unused but to be implemented due to abstract parent
  193. }
  194. private final class FeaturesWebViewAdapter extends FragmentPagerAdapter {
  195. private String[] mWebUrls;
  196. public FeaturesWebViewAdapter(FragmentManager fm, String[] webUrls) {
  197. super(fm);
  198. mWebUrls = webUrls;
  199. }
  200. @Override
  201. public Fragment getItem(int position) {
  202. return FeatureWebFragment.newInstance(mWebUrls[position]);
  203. }
  204. @Override
  205. public int getCount() {
  206. return mWebUrls.length;
  207. }
  208. }
  209. public static class FeatureWebFragment extends Fragment {
  210. private String mWebUrl;
  211. static public FeatureWebFragment newInstance(String webUrl) {
  212. FeatureWebFragment f = new FeatureWebFragment();
  213. Bundle args = new Bundle();
  214. args.putString("url", webUrl);
  215. f.setArguments(args);
  216. return f;
  217. }
  218. @Override
  219. public void onCreate(@Nullable Bundle savedInstanceState) {
  220. super.onCreate(savedInstanceState);
  221. mWebUrl = getArguments() != null ? getArguments().getString("url") : null;
  222. }
  223. @Nullable
  224. @Override
  225. public View onCreateView(LayoutInflater inflater,
  226. @Nullable ViewGroup container,
  227. @Nullable Bundle savedInstanceState) {
  228. View v = inflater.inflate(R.layout.whats_new_webview_element, container, false);
  229. WebView webView = (WebView) v.findViewById(R.id.whatsNewWebView);
  230. webView.getSettings().setJavaScriptEnabled(true);
  231. webView.getSettings().setDomStorageEnabled(true);
  232. webView.getSettings().setAllowFileAccess(false);
  233. webView.setWebViewClient(new WebViewClient());
  234. webView.loadUrl(mWebUrl);
  235. return v;
  236. }
  237. }
  238. private final class FeaturesViewAdapter extends FragmentPagerAdapter {
  239. private FeatureItem[] mFeatures;
  240. public FeaturesViewAdapter(FragmentManager fm, FeatureItem[] features) {
  241. super(fm);
  242. mFeatures = features;
  243. }
  244. @Override
  245. public Fragment getItem(int position) {
  246. return FeatureFragment.newInstance(mFeatures[position]);
  247. }
  248. @Override
  249. public int getCount() {
  250. return mFeatures.length;
  251. }
  252. }
  253. public static class FeatureFragment extends Fragment {
  254. private FeatureItem mItem;
  255. static public FeatureFragment newInstance(FeatureItem item) {
  256. FeatureFragment f = new FeatureFragment();
  257. Bundle args = new Bundle();
  258. args.putParcelable("feature", item);
  259. f.setArguments(args);
  260. return f;
  261. }
  262. @Override
  263. public void onCreate(@Nullable Bundle savedInstanceState) {
  264. super.onCreate(savedInstanceState);
  265. mItem = getArguments() != null ? (FeatureItem) getArguments().getParcelable("feature") : null;
  266. }
  267. @Nullable
  268. @Override
  269. public View onCreateView(LayoutInflater inflater,
  270. @Nullable ViewGroup container,
  271. @Nullable Bundle savedInstanceState) {
  272. View v = inflater.inflate(R.layout.whats_new_element, container, false);
  273. ImageView iv = (ImageView) v.findViewById(R.id.whatsNewImage);
  274. if (mItem.shouldShowImage()) {
  275. iv.setImageResource(mItem.getImage());
  276. }
  277. TextView tv2 = (TextView) v.findViewById(R.id.whatsNewTitle);
  278. if (mItem.shouldShowTitleText()) {
  279. tv2.setText(mItem.getTitleText());
  280. }
  281. tv2 = (TextView) v.findViewById(R.id.whatsNewText);
  282. if (mItem.shouldShowContentText()) {
  283. tv2.setText(mItem.getContentText());
  284. if (!mItem.shouldContentCentered()) {
  285. tv2.setGravity(Gravity.START);
  286. }
  287. }
  288. return v;
  289. }
  290. }
  291. }