From a181eac9a57c3b0ccf414c52d6d95e74d154c3d9 Mon Sep 17 00:00:00 2001 From: jendib Date: Mon, 1 Dec 2014 22:20:23 +0100 Subject: [PATCH] Android: settings activity --- docs-android/app/src/main/AndroidManifest.xml | 4 + .../com/sismics/docs/MainApplication.java | 1 - .../docs/activity/DocumentEditActivity.java | 6 +- .../docs/activity/DocumentViewActivity.java | 6 +- .../sismics/docs/activity/MainActivity.java | 33 ++++++- .../docs/activity/SettingsActivity.java | 39 ++++++++ .../docs/fragment/SettingsFragment.java | 88 +++++++++++++++++++ .../com/sismics/docs/util/PreferenceUtil.java | 1 + .../app/src/main/res/menu/main_activity.xml | 14 +++ .../app/src/main/res/values/arrays.xml | 18 ++++ .../app/src/main/res/values/strings.xml | 16 ++++ .../app/src/main/res/xml/preferences.xml | 57 +++++++++++- 12 files changed, 269 insertions(+), 14 deletions(-) create mode 100644 docs-android/app/src/main/java/com/sismics/docs/activity/SettingsActivity.java create mode 100644 docs-android/app/src/main/java/com/sismics/docs/fragment/SettingsFragment.java create mode 100644 docs-android/app/src/main/res/values/arrays.xml diff --git a/docs-android/app/src/main/AndroidManifest.xml b/docs-android/app/src/main/AndroidManifest.xml index fb1ccca4..b26ef828 100644 --- a/docs-android/app/src/main/AndroidManifest.xml +++ b/docs-android/app/src/main/AndroidManifest.xml @@ -38,6 +38,10 @@ android:name=".activity.DocumentEditActivity" android:label="@string/new_document"> + + diff --git a/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java b/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java index 17f3fe9d..3506654f 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java +++ b/docs-android/app/src/main/java/com/sismics/docs/MainApplication.java @@ -21,7 +21,6 @@ public class MainApplication extends Application { ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json); // TODO Fullscreen preview - // TODO Caching preferences // TODO Documents adding/editing // TODO Files adding/deleting diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java index 24661bb2..1ebb1771 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentEditActivity.java @@ -27,10 +27,8 @@ public class DocumentEditActivity extends ActionBarActivity { // Setup the activity setContentView(R.layout.document_edit_activity); - if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setHomeButtonEnabled(true); - } + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); Spinner languageSpinner = (Spinner) findViewById(R.id.languageSpinner); languageSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java index f7c5bc21..dec36ddb 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/DocumentViewActivity.java @@ -92,10 +92,8 @@ public class DocumentViewActivity extends ActionBarActivity { // Setup the activity setContentView(R.layout.document_view_activity); - if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setHomeButtonEnabled(true); - } + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); // Grab the document refreshDocument(document); diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java index a0c1bfef..fd8a8aba 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/MainActivity.java @@ -18,6 +18,7 @@ import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; +import com.androidquery.util.AQUtility; import com.sismics.docs.R; import com.sismics.docs.adapter.TagListAdapter; import com.sismics.docs.event.SearchEvent; @@ -25,6 +26,7 @@ import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.model.application.ApplicationContext; import com.sismics.docs.provider.RecentSuggestionsProvider; import com.sismics.docs.resource.TagResource; +import com.sismics.docs.resource.UserResource; import com.sismics.docs.util.PreferenceUtil; import org.apache.http.Header; @@ -59,10 +61,8 @@ public class MainActivity extends ActionBarActivity { // Enable ActionBar app icon to behave as action to toggle nav drawer drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); - if (getSupportActionBar() != null) { - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setHomeButtonEnabled(true); - } + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon @@ -139,6 +139,22 @@ public class MainActivity extends ActionBarActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.logout: + UserResource.logout(getApplicationContext(), new JsonHttpResponseHandler() { + @Override + public void onFinish() { + // Force logout in all cases, so the user is not stuck in case of network error + ApplicationContext.getInstance().setUserInfo(getApplicationContext(), null); + startActivity(new Intent(MainActivity.this, LoginActivity.class)); + finish(); + } + }); + return true; + + case R.id.settings: + startActivity(new Intent(MainActivity.this, SettingsActivity.class)); + return true; + case android.R.id.home: // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. @@ -233,4 +249,13 @@ public class MainActivity extends ActionBarActivity { searchView.clearFocus(); drawerLayout.closeDrawers(); } + + @Override + protected void onDestroy() { + if(isTaskRoot()) { + int cacheSizeMb = PreferenceUtil.getIntegerPreference(this, PreferenceUtil.PREF_CACHE_SIZE, 10); + AQUtility.cleanCacheAsync(this, cacheSizeMb * 1000000, cacheSizeMb * 1000000); + } + super.onDestroy(); + } } \ No newline at end of file diff --git a/docs-android/app/src/main/java/com/sismics/docs/activity/SettingsActivity.java b/docs-android/app/src/main/java/com/sismics/docs/activity/SettingsActivity.java new file mode 100644 index 00000000..510c3489 --- /dev/null +++ b/docs-android/app/src/main/java/com/sismics/docs/activity/SettingsActivity.java @@ -0,0 +1,39 @@ +package com.sismics.docs.activity; + +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.MenuItem; + +import com.sismics.docs.fragment.SettingsFragment; + +/** + * Settings activity. + * + * @author bgamard. + */ +public class SettingsActivity extends ActionBarActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); + + // Display the fragment as the main content. + getFragmentManager().beginTransaction() + .replace(android.R.id.content, new SettingsFragment()) + .commit(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/docs-android/app/src/main/java/com/sismics/docs/fragment/SettingsFragment.java b/docs-android/app/src/main/java/com/sismics/docs/fragment/SettingsFragment.java new file mode 100644 index 00000000..f3c67fa1 --- /dev/null +++ b/docs-android/app/src/main/java/com/sismics/docs/fragment/SettingsFragment.java @@ -0,0 +1,88 @@ +package com.sismics.docs.fragment; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; +import android.provider.SearchRecentSuggestions; +import android.widget.Toast; + +import com.androidquery.util.AQUtility; +import com.sismics.docs.R; +import com.sismics.docs.provider.RecentSuggestionsProvider; +import com.sismics.docs.util.ApplicationUtil; +import com.sismics.docs.util.PreferenceUtil; + +/** + * Settings fragment. + * + * @author bgamard. + */ +public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.preferences); + + // Initialize summaries + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + onSharedPreferenceChanged(sharedPreferences, PreferenceUtil.PREF_CACHE_SIZE); + + + // Handle clearing the recent search history + Preference clearHistoryPref = findPreference("pref_clearHistory"); + clearHistoryPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), + RecentSuggestionsProvider.AUTHORITY, RecentSuggestionsProvider.MODE); + suggestions.clearHistory(); + Toast.makeText(getActivity(), R.string.pref_clear_history_success, Toast.LENGTH_LONG).show(); + return true; + } + }); + + // Handle clearing the cache + Preference clearCachePref = findPreference("pref_clearCache"); + clearCachePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AQUtility.cleanCacheAsync(getActivity()); + Toast.makeText(getActivity(), R.string.pref_clear_cache_success, Toast.LENGTH_LONG).show(); + return true; + } + }); + + // Initialize static text preferences + Preference versionPref = findPreference("pref_version"); + versionPref.setSummary(getString(R.string.version) + " " + ApplicationUtil.getVersionName(getActivity()) + + " | " + getString(R.string.build) + " " + ApplicationUtil.getVersionCode(getActivity())); + } + + @Override + public void onResume() { + super.onResume(); + getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + + } + + @Override + public void onPause() { + getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); + super.onPause(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + Preference pref = findPreference(key); + if (pref instanceof ListPreference) { + ListPreference listPref = (ListPreference) pref; + pref.setSummary(listPref.getEntry()); + } + } +} diff --git a/docs-android/app/src/main/java/com/sismics/docs/util/PreferenceUtil.java b/docs-android/app/src/main/java/com/sismics/docs/util/PreferenceUtil.java index 09e16e06..629d7714 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/util/PreferenceUtil.java +++ b/docs-android/app/src/main/java/com/sismics/docs/util/PreferenceUtil.java @@ -22,6 +22,7 @@ public class PreferenceUtil { public static final String PREF_CACHED_USER_INFO_JSON = "pref_cachedUserInfoJson"; public static final String PREF_CACHED_TAGS_JSON = "pref_cachedTagsJson"; public static final String PREF_SERVER_URL = "pref_ServerUrl"; + public static final String PREF_CACHE_SIZE = "pref_cacheSize"; /** * Returns a preference of boolean type. diff --git a/docs-android/app/src/main/res/menu/main_activity.xml b/docs-android/app/src/main/res/menu/main_activity.xml index a635ec73..7f05ba85 100644 --- a/docs-android/app/src/main/res/menu/main_activity.xml +++ b/docs-android/app/src/main/res/menu/main_activity.xml @@ -1,8 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/docs-android/app/src/main/res/values/arrays.xml b/docs-android/app/src/main/res/values/arrays.xml new file mode 100644 index 00000000..df051d5b --- /dev/null +++ b/docs-android/app/src/main/res/values/arrays.xml @@ -0,0 +1,18 @@ + + + + + 5MB + 10MB + 30MB + 100MB + + + + 5 + 10 + 30 + 100 + + + \ No newline at end of file diff --git a/docs-android/app/src/main/res/values/strings.xml b/docs-android/app/src/main/res/values/strings.xml index eb18938d..e01f5f32 100644 --- a/docs-android/app/src/main/res/values/strings.xml +++ b/docs-android/app/src/main/res/values/strings.xml @@ -56,5 +56,21 @@ Send share link to Upload a file Upload a file from + Settings + Sign Out + Version + Build + Advanced settings + About + GitHub + Report a bug + Clear cache + Cleanup cached files + Cache cleared + Clear search history + Wipe the recent search suggestions + Search history cleared + Cache size + diff --git a/docs-android/app/src/main/res/xml/preferences.xml b/docs-android/app/src/main/res/xml/preferences.xml index 9beaa6c7..bdd57d34 100644 --- a/docs-android/app/src/main/res/xml/preferences.xml +++ b/docs-android/app/src/main/res/xml/preferences.xml @@ -1,3 +1,58 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file