Preferences.java 21 KB

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