Preferences.java 38 KB

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