Preferences.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.accounts.AccountManagerCallback;
  22. import android.accounts.AccountManagerFuture;
  23. import android.content.Intent;
  24. import android.content.SharedPreferences;
  25. import android.content.pm.PackageInfo;
  26. import android.content.pm.PackageManager.NameNotFoundException;
  27. import android.net.Uri;
  28. import android.os.Bundle;
  29. import android.os.Handler;
  30. import android.preference.CheckBoxPreference;
  31. import android.preference.Preference;
  32. import android.preference.Preference.OnPreferenceChangeListener;
  33. import android.preference.Preference.OnPreferenceClickListener;
  34. import android.preference.PreferenceCategory;
  35. import android.preference.PreferenceManager;
  36. import android.view.ContextMenu;
  37. import android.view.ContextMenu.ContextMenuInfo;
  38. import android.view.View;
  39. import android.widget.AdapterView;
  40. import android.widget.AdapterView.OnItemLongClickListener;
  41. import android.widget.ListAdapter;
  42. import android.widget.ListView;
  43. import com.actionbarsherlock.app.ActionBar;
  44. import com.actionbarsherlock.app.SherlockPreferenceActivity;
  45. import com.actionbarsherlock.view.Menu;
  46. import com.actionbarsherlock.view.MenuItem;
  47. import com.owncloud.android.MainApp;
  48. import com.owncloud.android.R;
  49. import com.owncloud.android.authentication.AccountUtils;
  50. import com.owncloud.android.authentication.AuthenticatorActivity;
  51. import com.owncloud.android.db.DbHandler;
  52. import com.owncloud.android.ui.LongClickableCheckBoxPreference;
  53. import com.owncloud.android.utils.DisplayUtils;
  54. import com.owncloud.android.utils.Log_OC;
  55. /**
  56. * An Activity that allows the user to change the application's settings.
  57. *
  58. * @author Bartek Przybylski
  59. * @author David A. Velasco
  60. */
  61. public class Preferences extends SherlockPreferenceActivity implements AccountManagerCallback<Boolean> {
  62. private static final String TAG = "OwnCloudPreferences";
  63. private DbHandler mDbHandler;
  64. private CheckBoxPreference pCode;
  65. //private CheckBoxPreference pLogging;
  66. //private Preference pLoggingHistory;
  67. private Preference pAboutApp;
  68. private PreferenceCategory mAccountsPrefCategory = null;
  69. private final Handler mHandler = new Handler();
  70. private String mAccountName;
  71. private boolean mShowContextMenu = false;
  72. @SuppressWarnings("deprecation")
  73. @Override
  74. public void onCreate(Bundle savedInstanceState) {
  75. super.onCreate(savedInstanceState);
  76. mDbHandler = new DbHandler(getBaseContext());
  77. addPreferencesFromResource(R.xml.preferences);
  78. ActionBar actionBar = getSherlock().getActionBar();
  79. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  80. actionBar.setDisplayHomeAsUpEnabled(true);
  81. // Load the accounts category for adding the list of accounts
  82. mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  83. ListView listView = getListView();
  84. listView.setOnItemLongClickListener(new OnItemLongClickListener() {
  85. @Override
  86. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  87. ListView listView = (ListView) parent;
  88. ListAdapter listAdapter = listView.getAdapter();
  89. Object obj = listAdapter.getItem(position);
  90. if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
  91. mShowContextMenu = true;
  92. mAccountName = obj.toString();
  93. Preferences.this.openContextMenu(listView);
  94. View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
  95. return longListener.onLongClick(view);
  96. }
  97. return false;
  98. }
  99. });
  100. // Register context menu for list of preferences.
  101. registerForContextMenu(getListView());
  102. pCode = (CheckBoxPreference) findPreference("set_pincode");
  103. if (pCode != null){
  104. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  105. @Override
  106. public boolean onPreferenceChange(Preference preference, Object newValue) {
  107. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  108. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
  109. i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
  110. startActivity(i);
  111. return true;
  112. }
  113. });
  114. }
  115. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  116. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  117. Preference pHelp = findPreference("help");
  118. if (pHelp != null ){
  119. if (helpEnabled) {
  120. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  121. @Override
  122. public boolean onPreferenceClick(Preference preference) {
  123. String helpWeb =(String) getText(R.string.url_help);
  124. if (helpWeb != null && helpWeb.length() > 0) {
  125. Uri uriUrl = Uri.parse(helpWeb);
  126. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  127. startActivity(intent);
  128. }
  129. return true;
  130. }
  131. });
  132. } else {
  133. preferenceCategory.removePreference(pHelp);
  134. }
  135. }
  136. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  137. Preference pRecommend = findPreference("recommend");
  138. if (pRecommend != null){
  139. if (recommendEnabled) {
  140. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  141. @Override
  142. public boolean onPreferenceClick(Preference preference) {
  143. Intent intent = new Intent(Intent.ACTION_SENDTO);
  144. intent.setType("text/plain");
  145. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  146. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  147. String appName = getString(R.string.app_name);
  148. String downloadUrl = getString(R.string.url_app_download);
  149. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
  150. String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
  151. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  152. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username);
  153. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  154. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  155. startActivity(intent);
  156. return(true);
  157. }
  158. });
  159. } else {
  160. preferenceCategory.removePreference(pRecommend);
  161. }
  162. }
  163. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  164. Preference pFeedback = findPreference("feedback");
  165. if (pFeedback != null){
  166. if (feedbackEnabled) {
  167. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  168. @Override
  169. public boolean onPreferenceClick(Preference preference) {
  170. String feedbackMail =(String) getText(R.string.mail_feedback);
  171. String feedback =(String) getText(R.string.prefs_feedback);
  172. Intent intent = new Intent(Intent.ACTION_SENDTO);
  173. intent.setType("text/plain");
  174. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  175. intent.setData(Uri.parse(feedbackMail));
  176. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  177. startActivity(intent);
  178. return true;
  179. }
  180. });
  181. } else {
  182. preferenceCategory.removePreference(pFeedback);
  183. }
  184. }
  185. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  186. Preference pImprint = findPreference("imprint");
  187. if (pImprint != null) {
  188. if (imprintEnabled) {
  189. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  190. @Override
  191. public boolean onPreferenceClick(Preference preference) {
  192. String imprintWeb = (String) getText(R.string.url_imprint);
  193. if (imprintWeb != null && imprintWeb.length() > 0) {
  194. Uri uriUrl = Uri.parse(imprintWeb);
  195. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  196. startActivity(intent);
  197. }
  198. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  199. return true;
  200. }
  201. });
  202. } else {
  203. preferenceCategory.removePreference(pImprint);
  204. }
  205. }
  206. /* About App */
  207. pAboutApp = (Preference) findPreference("about_app");
  208. if (pAboutApp != null) {
  209. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  210. PackageInfo pkg;
  211. try {
  212. pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  213. pAboutApp.setSummary(String.format(getString(R.string.about_version), pkg.versionName));
  214. } catch (NameNotFoundException e) {
  215. Log_OC.e(TAG, "Error while showing about dialog", e);
  216. }
  217. }
  218. /* DISABLED FOR RELEASE UNTIL FIXED
  219. pLogging = (CheckBoxPreference) findPreference("log_to_file");
  220. if (pLogging != null) {
  221. pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  222. @Override
  223. public boolean onPreferenceChange(Preference preference, Object newValue) {
  224. String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
  225. if(!pLogging.isChecked()) {
  226. Log_OC.d("Debug", "start logging");
  227. Log_OC.v("PATH", logpath);
  228. Log_OC.startLogging(logpath);
  229. }
  230. else {
  231. Log_OC.d("Debug", "stop logging");
  232. Log_OC.stopLogging();
  233. }
  234. return true;
  235. }
  236. });
  237. }
  238. pLoggingHistory = (Preference) findPreference("log_history");
  239. if (pLoggingHistory != null) {
  240. pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  241. @Override
  242. public boolean onPreferenceClick(Preference preference) {
  243. Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class);
  244. startActivity(intent);
  245. return true;
  246. }
  247. });
  248. }
  249. */
  250. }
  251. @Override
  252. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  253. // Filter for only showing contextual menu when long press on the
  254. // accounts
  255. if (mShowContextMenu) {
  256. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  257. mShowContextMenu = false;
  258. }
  259. super.onCreateContextMenu(menu, v, menuInfo);
  260. }
  261. /**
  262. * Called when the user clicked on an item into the context menu created at
  263. * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
  264. * every ownCloud {@link Account} , containing 'secondary actions' for them.
  265. *
  266. * {@inheritDoc}
  267. */
  268. @Override
  269. public boolean onContextItemSelected(android.view.MenuItem item) {
  270. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  271. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  272. for (Account a : accounts) {
  273. if (a.name.equals(mAccountName)) {
  274. if (item.getItemId() == R.id.change_password) {
  275. // Change account password
  276. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  277. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  278. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  279. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  280. startActivity(updateAccountCredentials);
  281. } else if (item.getItemId() == R.id.delete_account) {
  282. // Remove account
  283. am.removeAccount(a, this, mHandler);
  284. }
  285. }
  286. }
  287. return true;
  288. }
  289. @Override
  290. public void run(AccountManagerFuture<Boolean> future) {
  291. if (future.isDone()) {
  292. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  293. String accountName = "";
  294. if (a == null) {
  295. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  296. if (accounts.length != 0)
  297. accountName = accounts[0].name;
  298. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  299. }
  300. addAccountsCheckboxPreferences();
  301. }
  302. }
  303. @Override
  304. protected void onResume() {
  305. super.onResume();
  306. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  307. boolean state = appPrefs.getBoolean("set_pincode", false);
  308. pCode.setChecked(state);
  309. // Populate the accounts category with the list of accounts
  310. addAccountsCheckboxPreferences();
  311. }
  312. @Override
  313. public boolean onCreateOptionsMenu(Menu menu) {
  314. super.onCreateOptionsMenu(menu);
  315. return true;
  316. }
  317. @Override
  318. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  319. super.onMenuItemSelected(featureId, item);
  320. Intent intent;
  321. switch (item.getItemId()) {
  322. case android.R.id.home:
  323. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  324. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  325. startActivity(intent);
  326. break;
  327. default:
  328. Log_OC.w(TAG, "Unknown menu item triggered");
  329. return false;
  330. }
  331. return true;
  332. }
  333. @Override
  334. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  335. super.onActivityResult(requestCode, resultCode, data);
  336. }
  337. @Override
  338. protected void onDestroy() {
  339. mDbHandler.close();
  340. super.onDestroy();
  341. }
  342. /**
  343. * Create the list of accounts that has been added into the app
  344. */
  345. @SuppressWarnings("deprecation")
  346. private void addAccountsCheckboxPreferences() {
  347. // Remove accounts in case list is refreshing for avoiding to have
  348. // duplicate items
  349. if (mAccountsPrefCategory.getPreferenceCount() > 0) {
  350. mAccountsPrefCategory.removeAll();
  351. }
  352. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  353. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  354. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  355. if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
  356. // Show create account screen if there isn't any account
  357. am.addAccount(MainApp.getAccountType(), null, null, null, this,
  358. null,
  359. null);
  360. }
  361. else {
  362. for (Account a : accounts) {
  363. LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
  364. accountPreference.setKey(a.name);
  365. accountPreference.setTitle(a.name);
  366. mAccountsPrefCategory.addPreference(accountPreference);
  367. // Check the current account that is being used
  368. if (a.name.equals(currentAccount.name)) {
  369. accountPreference.setChecked(true);
  370. } else {
  371. accountPreference.setChecked(false);
  372. }
  373. accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  374. @Override
  375. public boolean onPreferenceChange(Preference preference, Object newValue) {
  376. String key = preference.getKey();
  377. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  378. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  379. for (Account a : accounts) {
  380. CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
  381. if (key.equals(a.name)) {
  382. boolean accountChanged = !p.isChecked();
  383. p.setChecked(true);
  384. AccountUtils.setCurrentOwnCloudAccount(
  385. getApplicationContext(),
  386. a.name
  387. );
  388. if (accountChanged) {
  389. // restart the main activity
  390. Intent i = new Intent(
  391. Preferences.this,
  392. FileDisplayActivity.class
  393. );
  394. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  395. startActivity(i);
  396. } else {
  397. finish();
  398. }
  399. } else {
  400. p.setChecked(false);
  401. }
  402. }
  403. return (Boolean) newValue;
  404. }
  405. });
  406. }
  407. // Add Create Account preference at the end of account list if
  408. // Multiaccount is enabled
  409. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  410. createAddAccountPreference();
  411. }
  412. }
  413. }
  414. /**
  415. * Create the preference for allow adding new accounts
  416. */
  417. private void createAddAccountPreference() {
  418. Preference addAccountPref = new Preference(this);
  419. addAccountPref.setKey("add_account");
  420. addAccountPref.setTitle(getString(R.string.prefs_add_account));
  421. mAccountsPrefCategory.addPreference(addAccountPref);
  422. addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  423. @Override
  424. public boolean onPreferenceClick(Preference preference) {
  425. AccountManager am = AccountManager.get(getApplicationContext());
  426. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
  427. return true;
  428. }
  429. });
  430. }
  431. }