Preferences.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import android.accounts.Account;
  20. import android.accounts.AccountManager;
  21. import android.content.Intent;
  22. import android.content.SharedPreferences;
  23. import android.content.pm.PackageInfo;
  24. import android.content.pm.PackageManager.NameNotFoundException;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.preference.CheckBoxPreference;
  28. import android.preference.Preference;
  29. import android.preference.Preference.OnPreferenceChangeListener;
  30. import android.preference.Preference.OnPreferenceClickListener;
  31. import android.preference.PreferenceCategory;
  32. import android.preference.PreferenceManager;
  33. import com.actionbarsherlock.app.ActionBar;
  34. import com.actionbarsherlock.app.SherlockPreferenceActivity;
  35. import com.actionbarsherlock.view.Menu;
  36. import com.actionbarsherlock.view.MenuItem;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.authentication.AccountUtils;
  40. import com.owncloud.android.db.DbHandler;
  41. import com.owncloud.android.ui.PreferenceMultiline;
  42. import com.owncloud.android.utils.DisplayUtils;
  43. import com.owncloud.android.utils.Log_OC;
  44. /**
  45. * An Activity that allows the user to change the application's settings.
  46. *
  47. * @author Bartek Przybylski
  48. * @author David A. Velasco
  49. */
  50. public class Preferences extends SherlockPreferenceActivity {
  51. private static final String TAG = "OwnCloudPreferences";
  52. private static final String PREVIOUS_ACCOUNT_KEY = "ACCOUNT";
  53. private DbHandler mDbHandler;
  54. private CheckBoxPreference pCode;
  55. //private CheckBoxPreference pLogging;
  56. //private Preference pLoggingHistory;
  57. private Preference pAboutApp;
  58. private Account mPreviousAccount = null;
  59. @SuppressWarnings("deprecation")
  60. @Override
  61. public void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. mDbHandler = new DbHandler(getBaseContext());
  64. addPreferencesFromResource(R.xml.preferences);
  65. //populateAccountList();
  66. ActionBar actionBar = getSherlock().getActionBar();
  67. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  68. actionBar.setDisplayHomeAsUpEnabled(true);
  69. if (savedInstanceState != null) {
  70. mPreviousAccount = savedInstanceState.getParcelable(PREVIOUS_ACCOUNT_KEY);
  71. } else {
  72. mPreviousAccount = AccountUtils.getCurrentOwnCloudAccount(this);
  73. }
  74. // Load the accounts category for adding the list of accounts
  75. PreferenceCategory accountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  76. // Populate the accounts category with the list of accounts
  77. createAccountsCheckboxPreferences(accountsPrefCategory);
  78. // Show Create Account if Multiaccount is enabled
  79. if (!getResources().getBoolean(R.bool.multiaccount_support)) {
  80. PreferenceMultiline addAccountPreference = (PreferenceMultiline) findPreference("add_account");
  81. accountsPrefCategory.removePreference(addAccountPreference);
  82. }
  83. Preference pAddAccount = findPreference("add_account");
  84. if (pAddAccount != null)
  85. pAddAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  86. @Override
  87. public boolean onPreferenceClick(Preference preference) {
  88. /*
  89. * Intent intent = new Intent(
  90. * android.provider.Settings.ACTION_ADD_ACCOUNT);
  91. * intent.putExtra("authorities", new String[] {
  92. * MainApp.getAuthTokenType() }); startActivity(intent);
  93. */
  94. AccountManager am = AccountManager.get(getApplicationContext());
  95. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
  96. return true;
  97. }
  98. });
  99. Preference pManageAccount = findPreference("manage_account");
  100. if (pManageAccount != null)
  101. pManageAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  102. @Override
  103. public boolean onPreferenceClick(Preference preference) {
  104. Intent i = new Intent(getApplicationContext(), AccountSelectActivity.class);
  105. startActivity(i);
  106. return true;
  107. }
  108. });
  109. pCode = (CheckBoxPreference) findPreference("set_pincode");
  110. if (pCode != null){
  111. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  112. @Override
  113. public boolean onPreferenceChange(Preference preference, Object newValue) {
  114. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  115. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
  116. i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
  117. startActivity(i);
  118. return true;
  119. }
  120. });
  121. }
  122. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  123. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  124. Preference pHelp = findPreference("help");
  125. if (pHelp != null ){
  126. if (helpEnabled) {
  127. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  128. @Override
  129. public boolean onPreferenceClick(Preference preference) {
  130. String helpWeb =(String) getText(R.string.url_help);
  131. if (helpWeb != null && helpWeb.length() > 0) {
  132. Uri uriUrl = Uri.parse(helpWeb);
  133. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  134. startActivity(intent);
  135. }
  136. return true;
  137. }
  138. });
  139. } else {
  140. preferenceCategory.removePreference(pHelp);
  141. }
  142. }
  143. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  144. Preference pRecommend = findPreference("recommend");
  145. if (pRecommend != null){
  146. if (recommendEnabled) {
  147. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  148. @Override
  149. public boolean onPreferenceClick(Preference preference) {
  150. Intent intent = new Intent(Intent.ACTION_SENDTO);
  151. intent.setType("text/plain");
  152. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  153. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  154. String appName = getString(R.string.app_name);
  155. String downloadUrl = getString(R.string.url_app_download);
  156. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
  157. String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
  158. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  159. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
  160. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  161. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  162. startActivity(intent);
  163. return(true);
  164. }
  165. });
  166. } else {
  167. preferenceCategory.removePreference(pRecommend);
  168. }
  169. }
  170. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  171. Preference pFeedback = findPreference("feedback");
  172. if (pFeedback != null){
  173. if (feedbackEnabled) {
  174. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  175. @Override
  176. public boolean onPreferenceClick(Preference preference) {
  177. String feedbackMail =(String) getText(R.string.mail_feedback);
  178. String feedback =(String) getText(R.string.prefs_feedback);
  179. Intent intent = new Intent(Intent.ACTION_SENDTO);
  180. intent.setType("text/plain");
  181. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  182. intent.setData(Uri.parse(feedbackMail));
  183. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  184. startActivity(intent);
  185. return true;
  186. }
  187. });
  188. } else {
  189. preferenceCategory.removePreference(pFeedback);
  190. }
  191. }
  192. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  193. Preference pImprint = findPreference("imprint");
  194. if (pImprint != null) {
  195. if (imprintEnabled) {
  196. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  197. @Override
  198. public boolean onPreferenceClick(Preference preference) {
  199. String imprintWeb = (String) getText(R.string.url_imprint);
  200. if (imprintWeb != null && imprintWeb.length() > 0) {
  201. Uri uriUrl = Uri.parse(imprintWeb);
  202. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  203. startActivity(intent);
  204. }
  205. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  206. return true;
  207. }
  208. });
  209. } else {
  210. preferenceCategory.removePreference(pImprint);
  211. }
  212. }
  213. /* About App */
  214. pAboutApp = (Preference) findPreference("about_app");
  215. if (pAboutApp != null) {
  216. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  217. PackageInfo pkg;
  218. try {
  219. pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  220. pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
  221. } catch (NameNotFoundException e) {
  222. Log_OC.e(TAG, "Error while showing about dialog", e);
  223. }
  224. }
  225. /* DISABLED FOR RELEASE UNTIL FIXED
  226. pLogging = (CheckBoxPreference) findPreference("log_to_file");
  227. if (pLogging != null) {
  228. pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  229. @Override
  230. public boolean onPreferenceChange(Preference preference, Object newValue) {
  231. String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
  232. if(!pLogging.isChecked()) {
  233. Log_OC.d("Debug", "start logging");
  234. Log_OC.v("PATH", logpath);
  235. Log_OC.startLogging(logpath);
  236. }
  237. else {
  238. Log_OC.d("Debug", "stop logging");
  239. Log_OC.stopLogging();
  240. }
  241. return true;
  242. }
  243. });
  244. }
  245. pLoggingHistory = (Preference) findPreference("log_history");
  246. if (pLoggingHistory != null) {
  247. pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  248. @Override
  249. public boolean onPreferenceClick(Preference preference) {
  250. Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
  251. startActivity(intent);
  252. return true;
  253. }
  254. });
  255. }
  256. */
  257. }
  258. @Override
  259. protected void onResume() {
  260. super.onResume();
  261. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  262. boolean state = appPrefs.getBoolean("set_pincode", false);
  263. pCode.setChecked(state);
  264. }
  265. @Override
  266. public boolean onCreateOptionsMenu(Menu menu) {
  267. super.onCreateOptionsMenu(menu);
  268. return true;
  269. }
  270. @Override
  271. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  272. super.onMenuItemSelected(featureId, item);
  273. Intent intent;
  274. switch (item.getItemId()) {
  275. case android.R.id.home:
  276. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  277. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  278. startActivity(intent);
  279. break;
  280. default:
  281. Log_OC.w(TAG, "Unknown menu item triggered");
  282. return false;
  283. }
  284. return true;
  285. }
  286. @Override
  287. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  288. super.onActivityResult(requestCode, resultCode, data);
  289. }
  290. @Override
  291. protected void onDestroy() {
  292. mDbHandler.close();
  293. super.onDestroy();
  294. }
  295. /**
  296. * Create the list of accounts that have been added into the app
  297. *
  298. * @param accountsPrefCategory
  299. */
  300. private void createAccountsCheckboxPreferences(PreferenceCategory accountsPrefCategory) {
  301. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  302. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  303. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  304. for (Account a : accounts) {
  305. CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
  306. checkBoxPreference.setKey(a.name);
  307. checkBoxPreference.setTitle(a.name);
  308. // Check the current account that is being used
  309. if (a.name.equals(currentAccount.name)) {
  310. checkBoxPreference.setChecked(true);
  311. }
  312. checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  313. @Override
  314. public boolean onPreferenceChange(Preference preference, Object newValue) {
  315. String key = preference.getKey();
  316. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  317. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  318. for (Account a : accounts) {
  319. @SuppressWarnings("deprecation")
  320. CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
  321. if (key.equals(a.name)) {
  322. p.setChecked(true);
  323. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
  324. } else {
  325. p.setChecked(false);
  326. }
  327. }
  328. return (Boolean) newValue;
  329. }
  330. });
  331. accountsPrefCategory.addPreference(checkBoxPreference);
  332. }
  333. }
  334. @Override
  335. protected void onPause() {
  336. if (this.isFinishing()) {
  337. Account current = AccountUtils.getCurrentOwnCloudAccount(this);
  338. if ((mPreviousAccount == null && current != null)
  339. || (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
  340. // the account set as default changed since this activity was
  341. // created
  342. // restart the main activity
  343. Intent i = new Intent(this, FileDisplayActivity.class);
  344. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  345. startActivity(i);
  346. }
  347. }
  348. super.onPause();
  349. }
  350. }