WhatsNewActivity.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.widget.Button;
  39. import android.widget.ImageButton;
  40. import android.widget.ImageView;
  41. import android.widget.TextView;
  42. import com.owncloud.android.MainApp;
  43. import com.owncloud.android.R;
  44. import com.owncloud.android.authentication.AccountAuthenticatorActivity;
  45. import com.owncloud.android.authentication.AccountUtils;
  46. import com.owncloud.android.features.FeatureList;
  47. import com.owncloud.android.features.FeatureList.FeatureItem;
  48. import com.owncloud.android.ui.whatsnew.ProgressIndicator;
  49. /**
  50. * Activity displaying general feature after a fresh install and new features after an update.
  51. */
  52. public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
  53. private static final String KEY_LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
  54. private static final String SCREEN_NAME = "What's new";
  55. private static final String TAG = WhatsNewActivity.class.getSimpleName();
  56. private ImageButton mForwardFinishButton;
  57. private Button mSkipButton;
  58. private ProgressIndicator mProgress;
  59. private ViewPager mPager;
  60. @Override
  61. protected void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.whats_new_activity);
  64. mProgress = (ProgressIndicator) findViewById(R.id.progressIndicator);
  65. mPager = (ViewPager)findViewById(R.id.contentPanel);
  66. final boolean isBeta = getResources().getBoolean(R.bool.is_beta);
  67. FeaturesViewAdapter adapter = new FeaturesViewAdapter(getSupportFragmentManager(),
  68. FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta));
  69. mProgress.setNumberOfSteps(adapter.getCount());
  70. mPager.setAdapter(adapter);
  71. mPager.addOnPageChangeListener(this);
  72. mForwardFinishButton = (ImageButton) findViewById(R.id.forward);
  73. mForwardFinishButton.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View view) {
  76. if (mProgress.hasNextStep()) {
  77. mPager.setCurrentItem(mPager.getCurrentItem()+1, true);
  78. mProgress.animateToStep(mPager.getCurrentItem()+1);
  79. } else {
  80. onFinish();
  81. finish();
  82. }
  83. updateNextButtonIfNeeded();
  84. }
  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 = (Button) findViewById(R.id.skip);
  92. mSkipButton.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View view) {
  95. onFinish();
  96. finish();
  97. }
  98. });
  99. TextView tv = (TextView)findViewById(R.id.welcomeText);
  100. tv.setText(isFirstRun() ? R.string.empty : R.string.whats_new_title);
  101. updateNextButtonIfNeeded();
  102. }
  103. @Override
  104. public void onBackPressed() {
  105. onFinish();
  106. super.onBackPressed();
  107. }
  108. private void updateNextButtonIfNeeded() {
  109. if (!mProgress.hasNextStep()) {
  110. mForwardFinishButton.setImageResource(R.drawable.ic_done_white);
  111. mSkipButton.setVisibility(View.INVISIBLE);
  112. } else {
  113. mForwardFinishButton.setImageResource(R.drawable.arrow_right);
  114. mSkipButton.setVisibility(View.VISIBLE);
  115. }
  116. }
  117. private void onFinish() {
  118. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  119. SharedPreferences.Editor editor = pref.edit();
  120. editor.putInt(KEY_LAST_SEEN_VERSION_CODE, MainApp.getVersionCode());
  121. editor.apply();
  122. }
  123. static private int getLastSeenVersionCode() {
  124. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
  125. return pref.getInt(KEY_LAST_SEEN_VERSION_CODE, 0);
  126. }
  127. static private boolean isFirstRun() {
  128. return getLastSeenVersionCode() == 0 && AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext()) == null;
  129. }
  130. static public void runIfNeeded(Context context) {
  131. if (context instanceof WhatsNewActivity) {
  132. return;
  133. }
  134. if (shouldShow(context)) {
  135. context.startActivity(new Intent(context, WhatsNewActivity.class));
  136. }
  137. }
  138. static private boolean shouldShow(Context context) {
  139. final boolean isBeta = context.getResources().getBoolean(R.bool.is_beta);
  140. return (isFirstRun() && context instanceof AccountAuthenticatorActivity) ||
  141. (
  142. !(isFirstRun() && (context instanceof FileDisplayActivity)) &&
  143. !(context instanceof PassCodeActivity) &&
  144. (FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta).length > 0)
  145. );
  146. }
  147. @Override
  148. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  149. // unused but to be implemented due to abstract parent
  150. }
  151. @Override
  152. public void onPageSelected(int position) {
  153. mProgress.animateToStep(position+1);
  154. updateNextButtonIfNeeded();
  155. }
  156. @Override
  157. public void onPageScrollStateChanged(int state) {
  158. // unused but to be implemented due to abstract parent
  159. }
  160. private final class FeaturesViewAdapter extends FragmentPagerAdapter {
  161. private FeatureItem[] mFeatures;
  162. public FeaturesViewAdapter(FragmentManager fm, FeatureItem[]features) {
  163. super(fm);
  164. mFeatures = features;
  165. }
  166. @Override
  167. public Fragment getItem(int position) {
  168. return FeatureFragment.newInstance(mFeatures[position]);
  169. }
  170. @Override
  171. public int getCount() {
  172. return mFeatures.length;
  173. }
  174. }
  175. public static class FeatureFragment extends Fragment {
  176. private FeatureItem mItem;
  177. static public FeatureFragment newInstance(FeatureItem item) {
  178. FeatureFragment f = new FeatureFragment();
  179. Bundle args = new Bundle();
  180. args.putParcelable("feature", item);
  181. f.setArguments(args);
  182. return f;
  183. }
  184. @Override
  185. public void onCreate(@Nullable Bundle savedInstanceState) {
  186. super.onCreate(savedInstanceState);
  187. mItem = getArguments() != null ? (FeatureItem)getArguments().getParcelable("feature") : null;
  188. }
  189. @Nullable
  190. @Override
  191. public View onCreateView(LayoutInflater inflater,
  192. @Nullable ViewGroup container,
  193. @Nullable Bundle savedInstanceState) {
  194. View v = inflater.inflate(R.layout.whats_new_element, container, false);
  195. ImageView iv = (ImageView)v.findViewById(R.id.whatsNewImage);
  196. if (mItem.shouldShowImage()) {
  197. iv.setImageResource(mItem.getImage());
  198. }
  199. TextView tv2 = (TextView)v.findViewById(R.id.whatsNewTitle);
  200. if (mItem.shouldShowTitleText()) {
  201. tv2.setText(mItem.getTitleText());
  202. }
  203. tv2 = (TextView)v.findViewById(R.id.whatsNewText);
  204. if (mItem.shouldShowContentText()) {
  205. tv2.setText(mItem.getContentText());
  206. }
  207. return v;
  208. }
  209. }
  210. }