Preferences.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2015 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.ComponentName;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.ServiceConnection;
  27. import android.content.SharedPreferences;
  28. import android.content.pm.PackageInfo;
  29. import android.content.pm.PackageManager.NameNotFoundException;
  30. import android.net.Uri;
  31. import android.os.Bundle;
  32. import android.os.Handler;
  33. import android.os.IBinder;
  34. import android.preference.CheckBoxPreference;
  35. import android.preference.Preference;
  36. import android.preference.Preference.OnPreferenceChangeListener;
  37. import android.preference.Preference.OnPreferenceClickListener;
  38. import android.preference.PreferenceCategory;
  39. import android.preference.PreferenceManager;
  40. import android.view.ContextMenu;
  41. import android.view.ContextMenu.ContextMenuInfo;
  42. import android.view.View;
  43. import android.widget.AdapterView;
  44. import android.widget.AdapterView.OnItemLongClickListener;
  45. import android.widget.ListAdapter;
  46. import android.widget.ListView;
  47. import com.actionbarsherlock.app.ActionBar;
  48. import com.actionbarsherlock.app.SherlockPreferenceActivity;
  49. import com.actionbarsherlock.view.Menu;
  50. import com.actionbarsherlock.view.MenuItem;
  51. import com.owncloud.android.MainApp;
  52. import com.owncloud.android.R;
  53. import com.owncloud.android.authentication.AccountUtils;
  54. import com.owncloud.android.authentication.AuthenticatorActivity;
  55. import com.owncloud.android.datamodel.FileDataStorageManager;
  56. import com.owncloud.android.datamodel.OCFile;
  57. import com.owncloud.android.db.DbHandler;
  58. import com.owncloud.android.files.FileOperationsHelper;
  59. import com.owncloud.android.files.services.FileDownloader;
  60. import com.owncloud.android.files.services.FileUploader;
  61. import com.owncloud.android.lib.common.utils.Log_OC;
  62. import com.owncloud.android.services.OperationsService;
  63. import com.owncloud.android.ui.RadioButtonPreference;
  64. import com.owncloud.android.utils.DisplayUtils;
  65. import java.io.File;
  66. /**
  67. * An Activity that allows the user to change the application's settings.
  68. *
  69. * @author Bartek Przybylski
  70. * @author David A. Velasco
  71. */
  72. public class Preferences extends SherlockPreferenceActivity
  73. implements AccountManagerCallback<Boolean>, ComponentsGetter {
  74. private static final String TAG = "OwnCloudPreferences";
  75. private static final int ACTION_SELECT_UPLOAD_PATH = 1;
  76. private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
  77. private DbHandler mDbHandler;
  78. private CheckBoxPreference pCode;
  79. private Preference pAboutApp;
  80. private PreferenceCategory mAccountsPrefCategory = null;
  81. private final Handler mHandler = new Handler();
  82. private String mAccountName;
  83. private boolean mShowContextMenu = false;
  84. private String mUploadPath;
  85. private PreferenceCategory mPrefInstantUploadCategory;
  86. private Preference mPrefInstantUpload;
  87. private Preference mPrefInstantUploadPath;
  88. private Preference mPrefInstantUploadPathWiFi;
  89. private Preference mPrefInstantVideoUpload;
  90. private Preference mPrefInstantVideoUploadPath;
  91. private Preference mPrefInstantVideoUploadPathWiFi;
  92. private String mUploadVideoPath;
  93. protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
  94. protected FileUploader.FileUploaderBinder mUploaderBinder = null;
  95. private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
  96. @SuppressWarnings("deprecation")
  97. @Override
  98. public void onCreate(Bundle savedInstanceState) {
  99. super.onCreate(savedInstanceState);
  100. mDbHandler = new DbHandler(getBaseContext());
  101. addPreferencesFromResource(R.xml.preferences);
  102. ActionBar actionBar = getSherlock().getActionBar();
  103. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  104. actionBar.setDisplayHomeAsUpEnabled(true);
  105. actionBar.setTitle(R.string.actionbar_settings);
  106. // Load the accounts category for adding the list of accounts
  107. mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
  108. ListView listView = getListView();
  109. listView.setOnItemLongClickListener(new OnItemLongClickListener() {
  110. @Override
  111. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  112. ListView listView = (ListView) parent;
  113. ListAdapter listAdapter = listView.getAdapter();
  114. Object obj = listAdapter.getItem(position);
  115. if (obj != null && obj instanceof RadioButtonPreference) {
  116. mShowContextMenu = true;
  117. mAccountName = ((RadioButtonPreference) obj).getKey();
  118. Preferences.this.openContextMenu(listView);
  119. View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
  120. return longListener.onLongClick(view);
  121. }
  122. return false;
  123. }
  124. });
  125. // Load package info
  126. String temp;
  127. try {
  128. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  129. temp = pkg.versionName;
  130. } catch (NameNotFoundException e) {
  131. temp = "";
  132. Log_OC.e(TAG, "Error while showing about dialog", e);
  133. }
  134. final String appVersion = temp;
  135. // Register context menu for list of preferences.
  136. registerForContextMenu(getListView());
  137. pCode = (CheckBoxPreference) findPreference("set_pincode");
  138. if (pCode != null){
  139. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  140. @Override
  141. public boolean onPreferenceChange(Preference preference, Object newValue) {
  142. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  143. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "preferences");
  144. i.putExtra(PinCodeActivity.EXTRA_NEW_STATE, newValue.toString());
  145. startActivity(i);
  146. return true;
  147. }
  148. });
  149. }
  150. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  151. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  152. Preference pHelp = findPreference("help");
  153. if (pHelp != null ){
  154. if (helpEnabled) {
  155. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  156. @Override
  157. public boolean onPreferenceClick(Preference preference) {
  158. String helpWeb =(String) getText(R.string.url_help);
  159. if (helpWeb != null && helpWeb.length() > 0) {
  160. Uri uriUrl = Uri.parse(helpWeb);
  161. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  162. startActivity(intent);
  163. }
  164. return true;
  165. }
  166. });
  167. } else {
  168. preferenceCategory.removePreference(pHelp);
  169. }
  170. }
  171. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  172. Preference pRecommend = findPreference("recommend");
  173. if (pRecommend != null){
  174. if (recommendEnabled) {
  175. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  176. @Override
  177. public boolean onPreferenceClick(Preference preference) {
  178. Intent intent = new Intent(Intent.ACTION_SENDTO);
  179. intent.setType("text/plain");
  180. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  181. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  182. String appName = getString(R.string.app_name);
  183. String downloadUrl = getString(R.string.url_app_download);
  184. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this);
  185. String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));
  186. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  187. String recommendText = String.format(getString(R.string.recommend_text),
  188. appName, downloadUrl, username);
  189. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  190. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  191. startActivity(intent);
  192. return(true);
  193. }
  194. });
  195. } else {
  196. preferenceCategory.removePreference(pRecommend);
  197. }
  198. }
  199. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  200. Preference pFeedback = findPreference("feedback");
  201. if (pFeedback != null){
  202. if (feedbackEnabled) {
  203. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  204. @Override
  205. public boolean onPreferenceClick(Preference preference) {
  206. String feedbackMail =(String) getText(R.string.mail_feedback);
  207. String feedback =(String) getText(R.string.prefs_feedback) + " - android v" + appVersion;
  208. Intent intent = new Intent(Intent.ACTION_SENDTO);
  209. intent.setType("text/plain");
  210. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  211. intent.setData(Uri.parse(feedbackMail));
  212. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  213. startActivity(intent);
  214. return true;
  215. }
  216. });
  217. } else {
  218. preferenceCategory.removePreference(pFeedback);
  219. }
  220. }
  221. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  222. Preference pImprint = findPreference("imprint");
  223. if (pImprint != null) {
  224. if (imprintEnabled) {
  225. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  226. @Override
  227. public boolean onPreferenceClick(Preference preference) {
  228. String imprintWeb = (String) getText(R.string.url_imprint);
  229. if (imprintWeb != null && imprintWeb.length() > 0) {
  230. Uri uriUrl = Uri.parse(imprintWeb);
  231. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  232. startActivity(intent);
  233. }
  234. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  235. return true;
  236. }
  237. });
  238. } else {
  239. preferenceCategory.removePreference(pImprint);
  240. }
  241. }
  242. mPrefInstantUploadPath = findPreference("instant_upload_path");
  243. if (mPrefInstantUploadPath != null){
  244. mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  245. @Override
  246. public boolean onPreferenceClick(Preference preference) {
  247. if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
  248. mUploadPath += OCFile.PATH_SEPARATOR;
  249. }
  250. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  251. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
  252. startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
  253. return true;
  254. }
  255. });
  256. }
  257. mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
  258. mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
  259. mPrefInstantUpload = findPreference("instant_uploading");
  260. toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
  261. mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  262. @Override
  263. public boolean onPreferenceChange(Preference preference, Object newValue) {
  264. toggleInstantPictureOptions((Boolean) newValue);
  265. return true;
  266. }
  267. });
  268. mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
  269. if (mPrefInstantVideoUploadPath != null){
  270. mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  271. @Override
  272. public boolean onPreferenceClick(Preference preference) {
  273. if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
  274. mUploadVideoPath += OCFile.PATH_SEPARATOR;
  275. }
  276. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  277. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadVideoPath);
  278. startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
  279. return true;
  280. }
  281. });
  282. }
  283. mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
  284. mPrefInstantVideoUpload = findPreference("instant_video_uploading");
  285. toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
  286. mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  287. @Override
  288. public boolean onPreferenceChange(Preference preference, Object newValue) {
  289. toggleInstantVideoOptions((Boolean) newValue);
  290. return true;
  291. }
  292. });
  293. /* About App */
  294. pAboutApp = (Preference) findPreference("about_app");
  295. if (pAboutApp != null) {
  296. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  297. pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
  298. }
  299. loadInstantUploadPath();
  300. loadInstantUploadVideoPath();
  301. /* ComponentsGetter */
  302. mDownloadServiceConnection = newTransferenceServiceConnection();
  303. if (mDownloadServiceConnection != null) {
  304. bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
  305. Context.BIND_AUTO_CREATE);
  306. }
  307. mUploadServiceConnection = newTransferenceServiceConnection();
  308. if (mUploadServiceConnection != null) {
  309. bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
  310. Context.BIND_AUTO_CREATE);
  311. }
  312. }
  313. private void toggleInstantPictureOptions(Boolean value){
  314. if (value){
  315. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
  316. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
  317. } else {
  318. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
  319. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
  320. }
  321. }
  322. private void toggleInstantVideoOptions(Boolean value){
  323. if (value){
  324. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
  325. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
  326. } else {
  327. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
  328. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
  329. }
  330. }
  331. @Override
  332. protected void onPause() {
  333. super.onPause();
  334. }
  335. @Override
  336. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  337. // Filter for only showing contextual menu when long press on the
  338. // accounts
  339. if (mShowContextMenu) {
  340. getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
  341. mShowContextMenu = false;
  342. }
  343. super.onCreateContextMenu(menu, v, menuInfo);
  344. }
  345. /**
  346. * Called when the user clicked on an item into the context menu created at
  347. * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for
  348. * every ownCloud {@link Account} , containing 'secondary actions' for them.
  349. *
  350. * {@inheritDoc}
  351. */
  352. @Override
  353. public boolean onContextItemSelected(android.view.MenuItem item) {
  354. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  355. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  356. for (Account a : accounts) {
  357. if (a.name.equals(mAccountName)) {
  358. if (item.getItemId() == R.id.change_password) {
  359. // Change account password
  360. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  361. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
  362. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  363. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  364. startActivity(updateAccountCredentials);
  365. } else if (item.getItemId() == R.id.delete_account) {
  366. // Remove account
  367. am.removeAccount(a, this, mHandler);
  368. Log_OC.d(TAG, "Remove an account " + a.name);
  369. }
  370. }
  371. }
  372. return true;
  373. }
  374. @Override
  375. public void run(AccountManagerFuture<Boolean> future) {
  376. if (future.isDone()) {
  377. // after remove account
  378. Account account = new Account(mAccountName, MainApp.getAccountType());
  379. if (!AccountUtils.exists(account, MainApp.getAppContext())) {
  380. // Cancel tranfers
  381. mUploaderBinder.cancel(account);
  382. mDownloaderBinder.cancel(account);
  383. }
  384. Account a = AccountUtils.getCurrentOwnCloudAccount(this);
  385. String accountName = "";
  386. if (a == null) {
  387. Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
  388. if (accounts.length != 0)
  389. accountName = accounts[0].name;
  390. AccountUtils.setCurrentOwnCloudAccount(this, accountName);
  391. }
  392. addAccountsCheckboxPreferences();
  393. }
  394. }
  395. @Override
  396. protected void onResume() {
  397. super.onResume();
  398. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  399. boolean state = appPrefs.getBoolean("set_pincode", false);
  400. pCode.setChecked(state);
  401. // Populate the accounts category with the list of accounts
  402. addAccountsCheckboxPreferences();
  403. }
  404. @Override
  405. public boolean onCreateOptionsMenu(Menu menu) {
  406. super.onCreateOptionsMenu(menu);
  407. return true;
  408. }
  409. @Override
  410. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  411. super.onMenuItemSelected(featureId, item);
  412. Intent intent;
  413. switch (item.getItemId()) {
  414. case android.R.id.home:
  415. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  416. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  417. startActivity(intent);
  418. break;
  419. default:
  420. Log_OC.w(TAG, "Unknown menu item triggered");
  421. return false;
  422. }
  423. return true;
  424. }
  425. @Override
  426. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  427. super.onActivityResult(requestCode, resultCode, data);
  428. if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
  429. OCFile folderToUpload = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  430. mUploadPath = folderToUpload.getRemotePath();
  431. mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
  432. // Show the path on summary preference
  433. mPrefInstantUploadPath.setSummary(mUploadPath);
  434. saveInstantUploadPathOnPreferences();
  435. } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK){
  436. OCFile folderToUploadVideo = (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  437. mUploadVideoPath = folderToUploadVideo.getRemotePath();
  438. mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
  439. // Show the video path on summary preference
  440. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  441. saveInstantUploadVideoPathOnPreferences();
  442. }
  443. }
  444. @Override
  445. protected void onDestroy() {
  446. mDbHandler.close();
  447. if (mDownloadServiceConnection != null) {
  448. unbindService(mDownloadServiceConnection);
  449. mDownloadServiceConnection = null;
  450. }
  451. if (mUploadServiceConnection != null) {
  452. unbindService(mUploadServiceConnection);
  453. mUploadServiceConnection = null;
  454. }
  455. super.onDestroy();
  456. }
  457. /**
  458. * Create the list of accounts that has been added into the app
  459. */
  460. @SuppressWarnings("deprecation")
  461. private void addAccountsCheckboxPreferences() {
  462. // Remove accounts in case list is refreshing for avoiding to have
  463. // duplicate items
  464. if (mAccountsPrefCategory.getPreferenceCount() > 0) {
  465. mAccountsPrefCategory.removeAll();
  466. }
  467. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  468. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  469. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  470. if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
  471. // Show create account screen if there isn't any account
  472. am.addAccount(MainApp.getAccountType(), null, null, null, this,
  473. null,
  474. null);
  475. }
  476. else {
  477. for (Account a : accounts) {
  478. RadioButtonPreference accountPreference = new RadioButtonPreference(this);
  479. accountPreference.setKey(a.name);
  480. // Handle internationalized domain names
  481. accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
  482. mAccountsPrefCategory.addPreference(accountPreference);
  483. // Check the current account that is being used
  484. if (a.name.equals(currentAccount.name)) {
  485. accountPreference.setChecked(true);
  486. } else {
  487. accountPreference.setChecked(false);
  488. }
  489. accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  490. @Override
  491. public boolean onPreferenceChange(Preference preference, Object newValue) {
  492. String key = preference.getKey();
  493. AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
  494. Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
  495. for (Account a : accounts) {
  496. RadioButtonPreference p = (RadioButtonPreference) findPreference(a.name);
  497. if (key.equals(a.name)) {
  498. boolean accountChanged = !p.isChecked();
  499. p.setChecked(true);
  500. AccountUtils.setCurrentOwnCloudAccount(
  501. getApplicationContext(),
  502. a.name
  503. );
  504. if (accountChanged) {
  505. // restart the main activity
  506. Intent i = new Intent(
  507. Preferences.this,
  508. FileDisplayActivity.class
  509. );
  510. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  511. startActivity(i);
  512. } else {
  513. finish();
  514. }
  515. } else {
  516. p.setChecked(false);
  517. }
  518. }
  519. return (Boolean) newValue;
  520. }
  521. });
  522. }
  523. // Add Create Account preference at the end of account list if
  524. // Multiaccount is enabled
  525. if (getResources().getBoolean(R.bool.multiaccount_support)) {
  526. createAddAccountPreference();
  527. }
  528. }
  529. }
  530. /**
  531. * Create the preference for allow adding new accounts
  532. */
  533. private void createAddAccountPreference() {
  534. Preference addAccountPref = new Preference(this);
  535. addAccountPref.setKey("add_account");
  536. addAccountPref.setTitle(getString(R.string.prefs_add_account));
  537. mAccountsPrefCategory.addPreference(addAccountPref);
  538. addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  539. @Override
  540. public boolean onPreferenceClick(Preference preference) {
  541. AccountManager am = AccountManager.get(getApplicationContext());
  542. am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
  543. return true;
  544. }
  545. });
  546. }
  547. /**
  548. * Load upload path set on preferences
  549. */
  550. private void loadInstantUploadPath() {
  551. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  552. mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
  553. mPrefInstantUploadPath.setSummary(mUploadPath);
  554. }
  555. /**
  556. * Save the "Instant Upload Path" on preferences
  557. */
  558. private void saveInstantUploadPathOnPreferences() {
  559. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  560. SharedPreferences.Editor editor = appPrefs.edit();
  561. editor.putString("instant_upload_path", mUploadPath);
  562. editor.commit();
  563. }
  564. /**
  565. * Load upload video path set on preferences
  566. */
  567. private void loadInstantUploadVideoPath() {
  568. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  569. mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
  570. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  571. }
  572. /**
  573. * Save the "Instant Video Upload Path" on preferences
  574. */
  575. private void saveInstantUploadVideoPathOnPreferences() {
  576. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  577. SharedPreferences.Editor editor = appPrefs.edit();
  578. editor.putString("instant_video_upload_path", mUploadVideoPath);
  579. editor.commit();
  580. }
  581. // Methods for ComponetsGetter
  582. @Override
  583. public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
  584. return mDownloaderBinder;
  585. }
  586. @Override
  587. public FileUploader.FileUploaderBinder getFileUploaderBinder() {
  588. return mUploaderBinder;
  589. }
  590. @Override
  591. public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
  592. return null;
  593. }
  594. @Override
  595. public FileDataStorageManager getStorageManager() {
  596. return null;
  597. }
  598. @Override
  599. public FileOperationsHelper getFileOperationsHelper() {
  600. return null;
  601. }
  602. protected ServiceConnection newTransferenceServiceConnection() {
  603. return new PreferencesServiceConnection();
  604. }
  605. /** Defines callbacks for service binding, passed to bindService() */
  606. private class PreferencesServiceConnection implements ServiceConnection {
  607. @Override
  608. public void onServiceConnected(ComponentName component, IBinder service) {
  609. if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
  610. mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
  611. } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
  612. Log_OC.d(TAG, "Upload service connected");
  613. mUploaderBinder = (FileUploader.FileUploaderBinder) service;
  614. } else {
  615. return;
  616. }
  617. }
  618. @Override
  619. public void onServiceDisconnected(ComponentName component) {
  620. if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
  621. Log_OC.d(TAG, "Download service suddenly disconnected");
  622. mDownloaderBinder = null;
  623. } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
  624. Log_OC.d(TAG, "Upload service suddenly disconnected");
  625. mUploaderBinder = null;
  626. }
  627. }
  628. };
  629. }