Preferences.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2011 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.ui.activity;
  23. import android.accounts.Account;
  24. import android.accounts.AccountManager;
  25. import android.accounts.AccountManagerCallback;
  26. import android.accounts.AccountManagerFuture;
  27. import android.content.ComponentName;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.content.ServiceConnection;
  31. import android.content.SharedPreferences;
  32. import android.content.pm.PackageInfo;
  33. import android.content.pm.PackageManager.NameNotFoundException;
  34. import android.content.res.Configuration;
  35. import android.net.Uri;
  36. import android.os.Bundle;
  37. import android.os.Handler;
  38. import android.os.IBinder;
  39. import android.preference.CheckBoxPreference;
  40. import android.preference.Preference;
  41. import android.preference.Preference.OnPreferenceChangeListener;
  42. import android.preference.Preference.OnPreferenceClickListener;
  43. import android.preference.PreferenceActivity;
  44. import android.preference.PreferenceCategory;
  45. import android.preference.PreferenceManager;
  46. import android.support.annotation.LayoutRes;
  47. import android.support.annotation.Nullable;
  48. import android.support.v7.app.ActionBar;
  49. import android.support.v7.app.AlertDialog;
  50. import android.support.v7.app.AppCompatDelegate;
  51. import android.support.v7.widget.Toolbar;
  52. import android.view.ContextMenu;
  53. import android.view.ContextMenu.ContextMenuInfo;
  54. import android.view.Menu;
  55. import android.view.MenuInflater;
  56. import android.view.MenuItem;
  57. import android.view.View;
  58. import android.view.ViewGroup;
  59. import android.widget.AdapterView;
  60. import android.widget.AdapterView.OnItemLongClickListener;
  61. import android.widget.ArrayAdapter;
  62. import android.widget.ListAdapter;
  63. import android.widget.ListView;
  64. import android.widget.Toast;
  65. import com.owncloud.android.BuildConfig;
  66. import com.owncloud.android.MainApp;
  67. import com.owncloud.android.R;
  68. import com.owncloud.android.authentication.AccountUtils;
  69. import com.owncloud.android.authentication.AuthenticatorActivity;
  70. import com.owncloud.android.datamodel.FileDataStorageManager;
  71. import com.owncloud.android.datamodel.OCFile;
  72. import com.owncloud.android.files.FileOperationsHelper;
  73. import com.owncloud.android.files.services.FileDownloader;
  74. import com.owncloud.android.files.services.FileUploader;
  75. import com.owncloud.android.lib.common.OwnCloudAccount;
  76. import com.owncloud.android.lib.common.utils.Log_OC;
  77. import com.owncloud.android.services.OperationsService;
  78. import com.owncloud.android.ui.RadioButtonPreference;
  79. import com.owncloud.android.utils.DisplayUtils;
  80. /**
  81. * An Activity that allows the user to change the application's settings.
  82. *
  83. * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
  84. * with AppCompat.
  85. */
  86. public class Preferences extends PreferenceActivity
  87. implements AccountManagerCallback<Boolean>, ComponentsGetter {
  88. private static final String TAG = Preferences.class.getSimpleName();
  89. private static final int ACTION_SELECT_UPLOAD_PATH = 1;
  90. private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
  91. private static final int ACTION_REQUEST_PASSCODE = 5;
  92. private static final int ACTION_CONFIRM_PASSCODE = 6;
  93. private CheckBoxPreference pCode;
  94. private Preference pAboutApp;
  95. private AppCompatDelegate mDelegate;
  96. private PreferenceCategory mAccountsPrefCategory = null;
  97. private final Handler mHandler = new Handler();
  98. private String mAccountName;
  99. private boolean mShowContextMenu = false;
  100. private String mUploadPath;
  101. private PreferenceCategory mPrefInstantUploadCategory;
  102. private Preference mPrefInstantUpload;
  103. private Preference mPrefInstantUploadBehaviour;
  104. private Preference mPrefInstantUploadPath;
  105. private Preference mPrefInstantUploadPathWiFi;
  106. private Preference mPrefInstantVideoUpload;
  107. private Preference mPrefInstantVideoUploadPath;
  108. private Preference mPrefInstantVideoUploadPathWiFi;
  109. private String mUploadVideoPath;
  110. protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
  111. protected FileUploader.FileUploaderBinder mUploaderBinder = null;
  112. private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
  113. @SuppressWarnings("deprecation")
  114. @Override
  115. public void onCreate(Bundle savedInstanceState) {
  116. getDelegate().installViewFactory();
  117. getDelegate().onCreate(savedInstanceState);
  118. super.onCreate(savedInstanceState);
  119. addPreferencesFromResource(R.xml.preferences);
  120. ActionBar actionBar = getSupportActionBar();
  121. actionBar.setDisplayHomeAsUpEnabled(true);
  122. actionBar.setTitle(R.string.actionbar_settings);
  123. // For adding content description tag to a title field in the action bar
  124. int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
  125. View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
  126. if (actionBarTitleView != null) { // it's null in Android 2.x
  127. getWindow().getDecorView().findViewById(actionBarTitleId).
  128. setContentDescription(getString(R.string.actionbar_settings));
  129. }
  130. // Load the accounts category for adding the list of accounts
  131. mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  132. ListView listView = getListView();
  133. listView.setOnItemLongClickListener(new OnItemLongClickListener() {
  134. @Override
  135. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  136. ListView listView = (ListView) parent;
  137. ListAdapter listAdapter = listView.getAdapter();
  138. Object obj = listAdapter.getItem(position);
  139. if (obj != null && obj instanceof RadioButtonPreference) {
  140. mShowContextMenu = true;
  141. mAccountName = ((RadioButtonPreference) obj).getKey();
  142. String[] items = {
  143. getResources().getString(R.string.change_password),
  144. getResources().getString(R.string.delete_account)
  145. };
  146. final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Preferences.this);
  147. View convertView = getLayoutInflater().inflate(R.layout.alert_dialog_list_view, null);
  148. alertDialogBuilder.setView(convertView);
  149. ListView lv = (ListView) convertView.findViewById(R.id.list);
  150. ArrayAdapter<String> adapter = new ArrayAdapter<String>(
  151. Preferences.this,R.layout.simple_dialog_list_item,items);
  152. lv.setAdapter(adapter);
  153. //Setup proper inline listener
  154. final AlertDialog alertDialog = alertDialogBuilder.create();
  155. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  156. @Override
  157. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  158. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  159. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  160. for (Account a : accounts) {
  161. if (a.name.equals(mAccountName)) {
  162. if (position==0) {
  163. // Change account password
  164. Intent updateAccountCredentials = new Intent(Preferences.this, AuthenticatorActivity.class);
  165. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  166. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  167. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  168. startActivity(updateAccountCredentials);
  169. alertDialog.cancel();
  170. } else if (position==1) {
  171. // Remove account
  172. am.removeAccount(a, Preferences.this, mHandler);
  173. Log_OC.d(TAG, "Remove an account " + a.name);
  174. alertDialog.cancel();
  175. }
  176. }
  177. }
  178. }
  179. });
  180. alertDialog.show();
  181. View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
  182. return longListener.onLongClick(view);
  183. }
  184. return false;
  185. }
  186. });
  187. // Load package info
  188. String temp;
  189. try {
  190. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  191. temp = pkg.versionName;
  192. } catch (NameNotFoundException e) {
  193. temp = "";
  194. Log_OC.e(TAG, "Error while showing about dialog", e);
  195. }
  196. final String appVersion = temp;
  197. // Register context menu for list of preferences.
  198. registerForContextMenu(getListView());
  199. pCode = (CheckBoxPreference) findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE);
  200. if (pCode != null){
  201. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  202. @Override
  203. public boolean onPreferenceChange(Preference preference, Object newValue) {
  204. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  205. Boolean incoming = (Boolean) newValue;
  206. i.setAction(
  207. incoming.booleanValue() ? PassCodeActivity.ACTION_REQUEST_WITH_RESULT :
  208. PassCodeActivity.ACTION_CHECK_WITH_RESULT
  209. );
  210. startActivityForResult(i, incoming.booleanValue() ? ACTION_REQUEST_PASSCODE :
  211. ACTION_CONFIRM_PASSCODE);
  212. // Don't update just yet, we will decide on it in onActivityResult
  213. return false;
  214. }
  215. });
  216. }
  217. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  218. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  219. Preference pHelp = findPreference("help");
  220. if (pHelp != null ){
  221. if (helpEnabled) {
  222. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  223. @Override
  224. public boolean onPreferenceClick(Preference preference) {
  225. String helpWeb =(String) getText(R.string.url_help);
  226. if (helpWeb != null && helpWeb.length() > 0) {
  227. Uri uriUrl = Uri.parse(helpWeb);
  228. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  229. startActivity(intent);
  230. }
  231. return true;
  232. }
  233. });
  234. } else {
  235. preferenceCategory.removePreference(pHelp);
  236. }
  237. }
  238. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  239. Preference pRecommend = findPreference("recommend");
  240. if (pRecommend != null){
  241. if (recommendEnabled) {
  242. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  243. @Override
  244. public boolean onPreferenceClick(Preference preference) {
  245. Intent intent = new Intent(Intent.ACTION_SENDTO);
  246. intent.setType("text/plain");
  247. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  248. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  249. String appName = getString(R.string.app_name);
  250. String downloadUrl = getString(R.string.url_app_download);
  251. String recommendSubject =
  252. String.format(getString(R.string.recommend_subject),
  253. appName);
  254. String recommendText = String.format(getString(R.string.recommend_text),
  255. appName, downloadUrl);
  256. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  257. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  258. startActivity(intent);
  259. return(true);
  260. }
  261. });
  262. } else {
  263. preferenceCategory.removePreference(pRecommend);
  264. }
  265. }
  266. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  267. Preference pFeedback = findPreference("feedback");
  268. if (pFeedback != null){
  269. if (feedbackEnabled) {
  270. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  271. @Override
  272. public boolean onPreferenceClick(Preference preference) {
  273. String feedbackMail =(String) getText(R.string.mail_feedback);
  274. String feedback =(String) getText(R.string.prefs_feedback) +
  275. " - android v" + appVersion;
  276. Intent intent = new Intent(Intent.ACTION_SENDTO);
  277. intent.setType("text/plain");
  278. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  279. intent.setData(Uri.parse(feedbackMail));
  280. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  281. startActivity(intent);
  282. return true;
  283. }
  284. });
  285. } else {
  286. preferenceCategory.removePreference(pFeedback);
  287. }
  288. }
  289. boolean loggerEnabled = getResources().getBoolean(R.bool.logger_enabled) || BuildConfig.DEBUG;
  290. Preference pLogger = findPreference("logger");
  291. if (pLogger != null){
  292. if (loggerEnabled) {
  293. pLogger.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  294. @Override
  295. public boolean onPreferenceClick(Preference preference) {
  296. Intent loggerIntent = new Intent(getApplicationContext(), LogHistoryActivity.class);
  297. startActivity(loggerIntent);
  298. return true;
  299. }
  300. });
  301. } else {
  302. preferenceCategory.removePreference(pLogger);
  303. }
  304. }
  305. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  306. Preference pImprint = findPreference("imprint");
  307. if (pImprint != null) {
  308. if (imprintEnabled) {
  309. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  310. @Override
  311. public boolean onPreferenceClick(Preference preference) {
  312. String imprintWeb = (String) getText(R.string.url_imprint);
  313. if (imprintWeb != null && imprintWeb.length() > 0) {
  314. Uri uriUrl = Uri.parse(imprintWeb);
  315. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  316. startActivity(intent);
  317. }
  318. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  319. return true;
  320. }
  321. });
  322. } else {
  323. preferenceCategory.removePreference(pImprint);
  324. }
  325. }
  326. mPrefInstantUploadPath = findPreference("instant_upload_path");
  327. if (mPrefInstantUploadPath != null){
  328. mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  329. @Override
  330. public boolean onPreferenceClick(Preference preference) {
  331. if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
  332. mUploadPath += OCFile.PATH_SEPARATOR;
  333. }
  334. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  335. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
  336. startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
  337. return true;
  338. }
  339. });
  340. }
  341. mPrefInstantUploadCategory =
  342. (PreferenceCategory) findPreference("instant_uploading_category");
  343. mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
  344. mPrefInstantUpload = findPreference("instant_uploading");
  345. toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
  346. mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  347. @Override
  348. public boolean onPreferenceChange(Preference preference, Object newValue) {
  349. toggleInstantPictureOptions((Boolean) newValue);
  350. toggleInstantUploadBehaviour(
  351. ((CheckBoxPreference)mPrefInstantVideoUpload).isChecked(),
  352. (Boolean) newValue);
  353. return true;
  354. }
  355. });
  356. mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
  357. if (mPrefInstantVideoUploadPath != null){
  358. mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  359. @Override
  360. public boolean onPreferenceClick(Preference preference) {
  361. if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
  362. mUploadVideoPath += OCFile.PATH_SEPARATOR;
  363. }
  364. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  365. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH,
  366. mUploadVideoPath);
  367. startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
  368. return true;
  369. }
  370. });
  371. }
  372. mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
  373. mPrefInstantVideoUpload = findPreference("instant_video_uploading");
  374. toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
  375. mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  376. @Override
  377. public boolean onPreferenceChange(Preference preference, Object newValue) {
  378. toggleInstantVideoOptions((Boolean) newValue);
  379. toggleInstantUploadBehaviour(
  380. (Boolean) newValue,
  381. ((CheckBoxPreference) mPrefInstantUpload).isChecked());
  382. return true;
  383. }
  384. });
  385. mPrefInstantUploadBehaviour = findPreference("prefs_instant_behaviour");
  386. toggleInstantUploadBehaviour(
  387. ((CheckBoxPreference)mPrefInstantVideoUpload).isChecked(),
  388. ((CheckBoxPreference)mPrefInstantUpload).isChecked());
  389. /* About App */
  390. pAboutApp = (Preference) findPreference("about_app");
  391. if (pAboutApp != null) {
  392. pAboutApp.setTitle(String.format(getString(R.string.about_android),
  393. getString(R.string.app_name)));
  394. pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
  395. }
  396. loadInstantUploadPath();
  397. loadInstantUploadVideoPath();
  398. /* ComponentsGetter */
  399. mDownloadServiceConnection = newTransferenceServiceConnection();
  400. if (mDownloadServiceConnection != null) {
  401. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  402. Context.BIND_AUTO_CREATE);
  403. }
  404. mUploadServiceConnection = newTransferenceServiceConnection();
  405. if (mUploadServiceConnection != null) {
  406. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  407. Context.BIND_AUTO_CREATE);
  408. }
  409. }
  410. private void toggleInstantPictureOptions(Boolean value){
  411. if (value){
  412. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
  413. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
  414. } else {
  415. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
  416. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
  417. }
  418. }
  419. private void toggleInstantVideoOptions(Boolean value){
  420. if (value){
  421. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
  422. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
  423. } else {
  424. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
  425. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
  426. }
  427. }
  428. private void toggleInstantUploadBehaviour(Boolean video, Boolean picture){
  429. if (picture || video){
  430. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadBehaviour);
  431. } else {
  432. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadBehaviour);
  433. }
  434. }
  435. @Override
  436. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  437. // Filter for only showing contextual menu when long press on the
  438. // accounts
  439. if (mShowContextMenu) {
  440. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  441. mShowContextMenu = false;
  442. }
  443. super.onCreateContextMenu(menu, v, menuInfo);
  444. }
  445. @Override
  446. public void run(AccountManagerFuture<Boolean> future) {
  447. if (future.isDone()) {
  448. // after remove account
  449. Account account = new Account(mAccountName, MainApp.getAccountType());
  450. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  451. // Cancel tranfers
  452. if (mUploaderBinder != null) {
  453. mUploaderBinder.cancel(account);
  454. }
  455. if (mDownloaderBinder != null) {
  456. mDownloaderBinder.cancel(account);
  457. }
  458. }
  459. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  460. String accountName = "";
  461. if (a == null) {
  462. Account[] accounts = AccountManager.get(this)
  463. .getAccountsByType(MainApp.getAccountType());
  464. if (accounts.length != 0)
  465. accountName = accounts[0].name;
  466. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  467. }
  468. addAccountsCheckboxPreferences();
  469. }
  470. }
  471. @Override
  472. protected void onResume() {
  473. super.onResume();
  474. SharedPreferences appPrefs =
  475. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  476. boolean state = appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  477. pCode.setChecked(state);
  478. // Populate the accounts category with the list of accounts
  479. addAccountsCheckboxPreferences();
  480. }
  481. @Override
  482. public boolean onCreateOptionsMenu(Menu menu) {
  483. super.onCreateOptionsMenu(menu);
  484. return true;
  485. }
  486. @Override
  487. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  488. super.onMenuItemSelected(featureId, item);
  489. Intent intent;
  490. switch (item.getItemId()) {
  491. case android.R.id.home:
  492. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  493. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  494. startActivity(intent);
  495. break;
  496. default:
  497. Log_OC.w(TAG, "Unknown menu item triggered");
  498. return false;
  499. }
  500. return true;
  501. }
  502. @Override
  503. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  504. super.onActivityResult(requestCode, resultCode, data);
  505. if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
  506. OCFile folderToUpload =
  507. (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  508. mUploadPath = folderToUpload.getRemotePath();
  509. mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
  510. // Show the path on summary preference
  511. mPrefInstantUploadPath.setSummary(mUploadPath);
  512. saveInstantUploadPathOnPreferences();
  513. } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
  514. OCFile folderToUploadVideo =
  515. (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  516. mUploadVideoPath = folderToUploadVideo.getRemotePath();
  517. mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
  518. // Show the video path on summary preference
  519. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  520. saveInstantUploadVideoPathOnPreferences();
  521. } else if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
  522. String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
  523. if (passcode != null && passcode.length() == 4) {
  524. SharedPreferences.Editor appPrefs = PreferenceManager
  525. .getDefaultSharedPreferences(getApplicationContext()).edit();
  526. for (int i = 1; i <= 4; ++i) {
  527. appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i-1, i));
  528. }
  529. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, true);
  530. appPrefs.commit();
  531. Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
  532. }
  533. } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
  534. if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
  535. SharedPreferences.Editor appPrefs = PreferenceManager
  536. .getDefaultSharedPreferences(getApplicationContext()).edit();
  537. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  538. appPrefs.commit();
  539. Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
  540. }
  541. }
  542. }
  543. public ActionBar getSupportActionBar() {
  544. return getDelegate().getSupportActionBar();
  545. }
  546. public void setSupportActionBar(@Nullable Toolbar toolbar) {
  547. getDelegate().setSupportActionBar(toolbar);
  548. }
  549. @Override
  550. public MenuInflater getMenuInflater() {
  551. return getDelegate().getMenuInflater();
  552. }
  553. @Override
  554. public void setContentView(@LayoutRes int layoutResID) {
  555. getDelegate().setContentView(layoutResID);
  556. }
  557. @Override
  558. public void setContentView(View view) {
  559. getDelegate().setContentView(view);
  560. }
  561. @Override
  562. public void setContentView(View view, ViewGroup.LayoutParams params) {
  563. getDelegate().setContentView(view, params);
  564. }
  565. @Override
  566. public void addContentView(View view, ViewGroup.LayoutParams params) {
  567. getDelegate().addContentView(view, params);
  568. }
  569. @Override
  570. protected void onPostResume() {
  571. super.onPostResume();
  572. getDelegate().onPostResume();
  573. }
  574. @Override
  575. protected void onTitleChanged(CharSequence title, int color) {
  576. super.onTitleChanged(title, color);
  577. getDelegate().setTitle(title);
  578. }
  579. @Override
  580. public void onConfigurationChanged(Configuration newConfig) {
  581. super.onConfigurationChanged(newConfig);
  582. getDelegate().onConfigurationChanged(newConfig);
  583. }
  584. @Override
  585. protected void onPostCreate(Bundle savedInstanceState) {
  586. super.onPostCreate(savedInstanceState);
  587. getDelegate().onPostCreate(savedInstanceState);
  588. }
  589. @Override
  590. protected void onDestroy() {
  591. if (mDownloadServiceConnection != null) {
  592. unbindService(mDownloadServiceConnection);
  593. mDownloadServiceConnection = null;
  594. }
  595. if (mUploadServiceConnection != null) {
  596. unbindService(mUploadServiceConnection);
  597. mUploadServiceConnection = null;
  598. }
  599. super.onDestroy();
  600. getDelegate().onDestroy();
  601. }
  602. @Override
  603. protected void onStop() {
  604. super.onStop();
  605. getDelegate().onStop();
  606. }
  607. public void invalidateOptionsMenu() {
  608. getDelegate().invalidateOptionsMenu();
  609. }
  610. private AppCompatDelegate getDelegate() {
  611. if (mDelegate == null) {
  612. mDelegate = AppCompatDelegate.create(this, null);
  613. }
  614. return mDelegate;
  615. }
  616. /**
  617. * Create the list of accounts that has been added into the app
  618. */
  619. @SuppressWarnings("deprecation")
  620. private void addAccountsCheckboxPreferences() {
  621. // Remove accounts in case list is refreshing for avoiding to have
  622. // duplicate items
  623. if (mAccountsPrefCategory.getPreferenceCount() > 0) {
  624. mAccountsPrefCategory.removeAll();
  625. }
  626. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  627. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  628. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  629. if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
  630. // Show create account screen if there isn't any account
  631. am.addAccount(MainApp.getAccountType(), null, null, null, this,
  632. null,
  633. null);
  634. }
  635. else {
  636. for (Account a : accounts) {
  637. RadioButtonPreference accountPreference = new RadioButtonPreference(this);
  638. accountPreference.setKey(a.name);
  639. try {
  640. OwnCloudAccount oca = new OwnCloudAccount(a, this);
  641. accountPreference.setTitle(oca.getDisplayName());
  642. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  643. Log_OC.w(
  644. TAG,
  645. "Account not found right after being read :\\ ; using account name instead of display name"
  646. );
  647. // Handle internationalized domain names
  648. accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
  649. }
  650. mAccountsPrefCategory.addPreference(accountPreference);
  651. // Check the current account that is being used
  652. if (a.name.equals(currentAccount.name)) {
  653. accountPreference.setChecked(true);
  654. } else {
  655. accountPreference.setChecked(false);
  656. }
  657. accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  658. @Override
  659. public boolean onPreferenceChange(Preference preference, Object newValue) {
  660. String key = preference.getKey();
  661. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  662. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  663. for (Account a : accounts) {
  664. RadioButtonPreference p =
  665. (RadioButtonPreference) findPreference(a.name);
  666. if (key.equals(a.name)) {
  667. boolean accountChanged = !p.isChecked();
  668. p.setChecked(true);
  669. AccountUtils.setCurrentOwnCloudAccount(
  670. getApplicationContext(),
  671. a.name
  672. );
  673. if (accountChanged) {
  674. // restart the main activity
  675. Intent i = new Intent(
  676. Preferences.this,
  677. FileDisplayActivity.class
  678. );
  679. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  680. i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  681. startActivity(i);
  682. } else {
  683. finish();
  684. }
  685. } else {
  686. p.setChecked(false);
  687. }
  688. }
  689. return (Boolean) newValue;
  690. }
  691. });
  692. }
  693. // Add Create Account preference at the end of account list if
  694. // Multiaccount is enabled
  695. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  696. createAddAccountPreference();
  697. }
  698. }
  699. }
  700. /**
  701. * Create the preference for allow adding new accounts
  702. */
  703. private void createAddAccountPreference() {
  704. Preference addAccountPref = new Preference(this);
  705. addAccountPref.setKey("add_account");
  706. addAccountPref.setTitle(getString(R.string.prefs_add_account));
  707. mAccountsPrefCategory.addPreference(addAccountPref);
  708. addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  709. @Override
  710. public boolean onPreferenceClick(Preference preference) {
  711. AccountManager am = AccountManager.get(getApplicationContext());
  712. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
  713. null, null);
  714. return true;
  715. }
  716. });
  717. }
  718. /**
  719. * Load upload path set on preferences
  720. */
  721. private void loadInstantUploadPath() {
  722. SharedPreferences appPrefs =
  723. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  724. mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
  725. mPrefInstantUploadPath.setSummary(mUploadPath);
  726. }
  727. /**
  728. * Save the "Instant Upload Path" on preferences
  729. */
  730. private void saveInstantUploadPathOnPreferences() {
  731. SharedPreferences appPrefs =
  732. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  733. SharedPreferences.Editor editor = appPrefs.edit();
  734. editor.putString("instant_upload_path", mUploadPath);
  735. editor.commit();
  736. }
  737. /**
  738. * Load upload video path set on preferences
  739. */
  740. private void loadInstantUploadVideoPath() {
  741. SharedPreferences appPrefs =
  742. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  743. mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
  744. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  745. }
  746. /**
  747. * Save the "Instant Video Upload Path" on preferences
  748. */
  749. private void saveInstantUploadVideoPathOnPreferences() {
  750. SharedPreferences appPrefs =
  751. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  752. SharedPreferences.Editor editor = appPrefs.edit();
  753. editor.putString("instant_video_upload_path", mUploadVideoPath);
  754. editor.commit();
  755. }
  756. // Methods for ComponetsGetter
  757. @Override
  758. public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
  759. return mDownloaderBinder;
  760. }
  761. @Override
  762. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  763. return mUploaderBinder;
  764. }
  765. @Override
  766. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  767. return null;
  768. }
  769. @Override
  770. public FileDataStorageManager getStorageManager() {
  771. return null;
  772. }
  773. @Override
  774. public FileOperationsHelper getFileOperationsHelper() {
  775. return null;
  776. }
  777. protected ServiceConnection newTransferenceServiceConnection() {
  778. return new PreferencesServiceConnection();
  779. }
  780. /** Defines callbacks for service binding, passed to bindService() */
  781. private class PreferencesServiceConnection implements ServiceConnection {
  782. @Override
  783. public void onServiceConnected(ComponentName component, IBinder service) {
  784. if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
  785. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  786. } else if (component.equals(new ComponentName(Preferences.this,
  787. FileUploader.class))) {
  788. Log_OC.d(TAG, "Upload service connected");
  789. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  790. } else {
  791. return;
  792. }
  793. }
  794. @Override
  795. public void onServiceDisconnected(ComponentName component) {
  796. if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
  797. Log_OC.d(TAG, "Download service suddenly disconnected");
  798. mDownloaderBinder = null;
  799. } else if (component.equals(new ComponentName(Preferences.this,
  800. FileUploader.class))) {
  801. Log_OC.d(TAG, "Upload service suddenly disconnected");
  802. mUploaderBinder = null;
  803. }
  804. }
  805. };
  806. }