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