WhatsNewActivity.java 11 KB

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