Add unfinished local directories browser for storage target. Add migration activity...
[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) getText(R.string.prefs_feedback) + " - android v" + appVersion;
335 Intent intent = new Intent(Intent.ACTION_SENDTO);
336 intent.setType("text/plain");
337 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
338
339 intent.setData(Uri.parse(feedbackMail));
340 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
341 startActivity(intent);
342
343 return true;
344 }
345 });
346 } else {
347 preferenceCategory.removePreference(pFeedback);
348 }
349
350 }
351
352 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
353 Preference pImprint = findPreference("imprint");
354 if (pImprint != null) {
355 if (imprintEnabled) {
356 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
357 @Override
358 public boolean onPreferenceClick(Preference preference) {
359 String imprintWeb = (String) getText(R.string.url_imprint);
360 if (imprintWeb != null && imprintWeb.length() > 0) {
361 Uri uriUrl = Uri.parse(imprintWeb);
362 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
363 startActivity(intent);
364 }
365 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
366 return true;
367 }
368 });
369 } else {
370 preferenceCategory.removePreference(pImprint);
371 }
372 }
373
374 mPrefStoragePath = findPreference("storage_path");
375 if (mPrefStoragePath != null) {
376
377 mPrefStoragePath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
378 @Override
379 public boolean onPreferenceClick(Preference preference) {
380 Intent intent = new Intent(Preferences.this, LocalDirectorySelectorActivity.class);
381 intent.putExtra(UploadFilesActivity.KEY_DIRECTORY_PATH, mStoragePath);
382 startActivityForResult(intent, ACTION_SELECT_STORAGE_PATH);
383 return true;
384 }
385 });
386
387 mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
388 @Override
389 public boolean onPreferenceChange(Preference preference, Object newValue) {
390 MainApp.setStoragePath((String) newValue);
391 return true;
392 }
393 });
394 }
395
396 mPrefInstantUploadPath = findPreference("instant_upload_path");
397 if (mPrefInstantUploadPath != null){
398
399 mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
400 @Override
401 public boolean onPreferenceClick(Preference preference) {
402 if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
403 mUploadPath += OCFile.PATH_SEPARATOR;
404 }
405 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
406 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
407 startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
408 return true;
409 }
410 });
411 }
412
413 mPrefInstantUploadCategory =
414 (PreferenceCategory) findPreference("instant_uploading_category");
415
416 mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
417 mPrefInstantUpload = findPreference("instant_uploading");
418
419 toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
420
421 mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
422
423 @Override
424 public boolean onPreferenceChange(Preference preference, Object newValue) {
425 toggleInstantPictureOptions((Boolean) newValue);
426 return true;
427 }
428 });
429
430 mPrefInstantVideoUploadPath = findPreference("instant_video_upload_path");
431 if (mPrefInstantVideoUploadPath != null){
432
433 mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
434 @Override
435 public boolean onPreferenceClick(Preference preference) {
436 if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
437 mUploadVideoPath += OCFile.PATH_SEPARATOR;
438 }
439 Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
440 intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH,
441 mUploadVideoPath);
442 startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
443 return true;
444 }
445 });
446 }
447
448 mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
449 mPrefInstantVideoUpload = findPreference("instant_video_uploading");
450 toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
451
452 mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
453
454 @Override
455 public boolean onPreferenceChange(Preference preference, Object newValue) {
456 toggleInstantVideoOptions((Boolean) newValue);
457 return true;
458 }
459 });
460
461 /* About App */
462 pAboutApp = (Preference) findPreference("about_app");
463 if (pAboutApp != null) {
464 pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
465 pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
466 }
467
468 loadInstantUploadPath();
469 loadStoragePath();
470 loadInstantUploadVideoPath();
471
472 /* ComponentsGetter */
473 mDownloadServiceConnection = newTransferenceServiceConnection();
474 if (mDownloadServiceConnection != null) {
475 bindService(new Intent(this, FileDownloader.class), mDownloadServiceConnection,
476 Context.BIND_AUTO_CREATE);
477 }
478 mUploadServiceConnection = newTransferenceServiceConnection();
479 if (mUploadServiceConnection != null) {
480 bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
481 Context.BIND_AUTO_CREATE);
482 }
483
484 }
485
486 private void toggleInstantPictureOptions(Boolean value){
487 if (value){
488 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
489 mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
490 } else {
491 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
492 mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
493 }
494 }
495
496 private void toggleInstantVideoOptions(Boolean value){
497 if (value){
498 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
499 mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
500 } else {
501 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
502 mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
503 }
504 }
505
506 @Override
507 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
508
509 // Filter for only showing contextual menu when long press on the
510 // accounts
511 if (mShowContextMenu) {
512 getMenuInflater().inflate(R.menu.account_picker_long_click, menu);
513 mShowContextMenu = false;
514 }
515 super.onCreateContextMenu(menu, v, menuInfo);
516 }
517
518 @Override
519 public void run(AccountManagerFuture<Boolean> future) {
520 if (future.isDone()) {
521 // after remove account
522 Account account = new Account(mAccountName, MainApp.getAccountType());
523 if (!AccountUtils.exists(account, MainApp.getAppContext())) {
524 // Cancel tranfers
525 if (mUploaderBinder != null) {
526 mUploaderBinder.cancel(account);
527 }
528 if (mDownloaderBinder != null) {
529 mDownloaderBinder.cancel(account);
530 }
531 }
532
533 Account a = AccountUtils.getCurrentOwnCloudAccount(this);
534 String accountName = "";
535 if (a == null) {
536 Account[] accounts = AccountManager.get(this)
537 .getAccountsByType(MainApp.getAccountType());
538 if (accounts.length != 0)
539 accountName = accounts[0].name;
540 AccountUtils.setCurrentOwnCloudAccount(this, accountName);
541 }
542 addAccountsCheckboxPreferences();
543 }
544 }
545
546 @Override
547 protected void onResume() {
548 super.onResume();
549 SharedPreferences appPrefs =
550 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
551 boolean state = appPrefs.getBoolean("set_pincode", false);
552 pCode.setChecked(state);
553
554 // Populate the accounts category with the list of accounts
555 addAccountsCheckboxPreferences();
556 }
557
558 @Override
559 public boolean onCreateOptionsMenu(Menu menu) {
560 super.onCreateOptionsMenu(menu);
561 return true;
562 }
563
564 @Override
565 public boolean onMenuItemSelected(int featureId, MenuItem item) {
566 super.onMenuItemSelected(featureId, item);
567 Intent intent;
568
569 switch (item.getItemId()) {
570 case android.R.id.home:
571 intent = new Intent(getBaseContext(), FileDisplayActivity.class);
572 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
573 startActivity(intent);
574 break;
575 default:
576 Log_OC.w(TAG, "Unknown menu item triggered");
577 return false;
578 }
579 return true;
580 }
581
582 @Override
583 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
584 super.onActivityResult(requestCode, resultCode, data);
585
586 if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
587
588 OCFile folderToUpload =
589 (OCFile) 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 =
603 (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
604
605 mUploadVideoPath = folderToUploadVideo.getRemotePath();
606
607 mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
608
609 // Show the video path on summary preference
610 mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
611
612 saveInstantUploadVideoPathOnPreferences();
613 } else if (requestCode == ACTION_SELECT_STORAGE_PATH && resultCode == RESULT_OK) {
614 File currentStorageDir = new File(mStoragePath);
615 File upcomingStorageDir = new File(data.getStringExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES));
616
617 if (currentStorageDir != upcomingStorageDir) {
618 Intent migrationIntent = new Intent(this, StorageMigrationActivity.class);
619 migrationIntent.putExtra(StorageMigrationActivity.KEY_MIGRATION_SOURCE_DIR,
620 currentStorageDir.getAbsolutePath());
621 migrationIntent.putExtra(StorageMigrationActivity.KEY_MIGRATION_TARGET_DIR,
622 currentStorageDir.getAbsolutePath());
623 startActivityForResult(migrationIntent, ACTION_PERFORM_MIGRATION);
624 }
625 } else if (requestCode == ACTION_PERFORM_MIGRATION && resultCode == RESULT_OK) {
626 String resultStorageDir = data.getStringExtra(StorageMigrationActivity.KEY_MIGRATION_TARGET_DIR);
627 mStoragePath = resultStorageDir;
628 mPrefStoragePath.setSummary(mStoragePath);
629 }
630 }
631
632 public ActionBar getSupportActionBar() {
633 return getDelegate().getSupportActionBar();
634 }
635
636 public void setSupportActionBar(@Nullable Toolbar toolbar) {
637 getDelegate().setSupportActionBar(toolbar);
638 }
639
640 @Override
641 public MenuInflater getMenuInflater() {
642 return getDelegate().getMenuInflater();
643 }
644
645 @Override
646 public void setContentView(@LayoutRes int layoutResID) {
647 getDelegate().setContentView(layoutResID);
648 }
649 @Override
650 public void setContentView(View view) {
651 getDelegate().setContentView(view);
652 }
653 @Override
654 public void setContentView(View view, ViewGroup.LayoutParams params) {
655 getDelegate().setContentView(view, params);
656 }
657
658 @Override
659 public void addContentView(View view, ViewGroup.LayoutParams params) {
660 getDelegate().addContentView(view, params);
661 }
662
663 @Override
664 protected void onPostResume() {
665 super.onPostResume();
666 getDelegate().onPostResume();
667 }
668
669 @Override
670 protected void onTitleChanged(CharSequence title, int color) {
671 super.onTitleChanged(title, color);
672 getDelegate().setTitle(title);
673 }
674
675 @Override
676 public void onConfigurationChanged(Configuration newConfig) {
677 super.onConfigurationChanged(newConfig);
678 getDelegate().onConfigurationChanged(newConfig);
679 }
680
681 @Override
682 protected void onPostCreate(Bundle savedInstanceState) {
683 super.onPostCreate(savedInstanceState);
684 getDelegate().onPostCreate(savedInstanceState);
685 }
686
687 @Override
688 protected void onDestroy() {
689 mDbHandler.close();
690
691 if (mDownloadServiceConnection != null) {
692 unbindService(mDownloadServiceConnection);
693 mDownloadServiceConnection = null;
694 }
695 if (mUploadServiceConnection != null) {
696 unbindService(mUploadServiceConnection);
697 mUploadServiceConnection = null;
698 }
699
700 super.onDestroy();
701 getDelegate().onDestroy();
702 }
703
704 @Override
705 protected void onStop() {
706 super.onStop();
707 getDelegate().onStop();
708 }
709
710 public void invalidateOptionsMenu() {
711 getDelegate().invalidateOptionsMenu();
712 }
713
714 private AppCompatDelegate getDelegate() {
715 if (mDelegate == null) {
716 mDelegate = AppCompatDelegate.create(this, null);
717 }
718 return mDelegate;
719 }
720
721 /**
722 * Create the list of accounts that has been added into the app
723 */
724 @SuppressWarnings("deprecation")
725 private void addAccountsCheckboxPreferences() {
726
727 // Remove accounts in case list is refreshing for avoiding to have
728 // duplicate items
729 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
730 mAccountsPrefCategory.removeAll();
731 }
732
733 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
734 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
735 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
736
737 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
738 // Show create account screen if there isn't any account
739 am.addAccount(MainApp.getAccountType(), null, null, null, this,
740 null,
741 null);
742 }
743 else {
744
745 for (Account a : accounts) {
746 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
747 accountPreference.setKey(a.name);
748 // Handle internationalized domain names
749 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
750 mAccountsPrefCategory.addPreference(accountPreference);
751
752 // Check the current account that is being used
753 if (a.name.equals(currentAccount.name)) {
754 accountPreference.setChecked(true);
755 } else {
756 accountPreference.setChecked(false);
757 }
758
759 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
760 @Override
761 public boolean onPreferenceChange(Preference preference, Object newValue) {
762 String key = preference.getKey();
763 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
764 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
765 for (Account a : accounts) {
766 RadioButtonPreference p =
767 (RadioButtonPreference) findPreference(a.name);
768 if (key.equals(a.name)) {
769 boolean accountChanged = !p.isChecked();
770 p.setChecked(true);
771 AccountUtils.setCurrentOwnCloudAccount(
772 getApplicationContext(),
773 a.name
774 );
775 if (accountChanged) {
776 // restart the main activity
777 Intent i = new Intent(
778 Preferences.this,
779 FileDisplayActivity.class
780 );
781 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
782 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
783 startActivity(i);
784 } else {
785 finish();
786 }
787 } else {
788 p.setChecked(false);
789 }
790 }
791 return (Boolean) newValue;
792 }
793 });
794
795 }
796
797 // Add Create Account preference at the end of account list if
798 // Multiaccount is enabled
799 if (getResources().getBoolean(R.bool.multiaccount_support)) {
800 createAddAccountPreference();
801 }
802
803 }
804 }
805
806 /**
807 * Create the preference for allow adding new accounts
808 */
809 private void createAddAccountPreference() {
810 Preference addAccountPref = new Preference(this);
811 addAccountPref.setKey("add_account");
812 addAccountPref.setTitle(getString(R.string.prefs_add_account));
813 mAccountsPrefCategory.addPreference(addAccountPref);
814
815 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
816 @Override
817 public boolean onPreferenceClick(Preference preference) {
818 AccountManager am = AccountManager.get(getApplicationContext());
819 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
820 null, null);
821 return true;
822 }
823 });
824
825 }
826
827 /**
828 * Load upload path set on preferences
829 */
830 private void loadInstantUploadPath() {
831 SharedPreferences appPrefs =
832 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
833 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
834 mPrefInstantUploadPath.setSummary(mUploadPath);
835 }
836
837 /**
838 * Load storage path set on preferences
839 */
840 private void loadStoragePath() {
841 SharedPreferences appPrefs =
842 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
843 mStoragePath = appPrefs.getString("storage_path", Environment.getExternalStorageDirectory()
844 .getAbsolutePath());
845 mPrefStoragePath.setSummary(mStoragePath + File.separator + MainApp.getDataFolder());
846 }
847
848 /**
849 * Save the "Instant Upload Path" on preferences
850 */
851 private void saveInstantUploadPathOnPreferences() {
852 SharedPreferences appPrefs =
853 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
854 SharedPreferences.Editor editor = appPrefs.edit();
855 editor.putString("instant_upload_path", mUploadPath);
856 editor.commit();
857 }
858
859 /**
860 * Load upload video path set on preferences
861 */
862 private void loadInstantUploadVideoPath() {
863 mPrefInstantVideoUploadPath.setSummary(MainApp.getStoragePath());
864 }
865
866 /**
867 * Save the "Instant Video Upload Path" on preferences
868 */
869 private void saveInstantUploadVideoPathOnPreferences() {
870 SharedPreferences appPrefs =
871 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
872 SharedPreferences.Editor editor = appPrefs.edit();
873 editor.putString("instant_video_upload_path", mUploadVideoPath);
874 editor.commit();
875 }
876
877 // Methods for ComponetsGetter
878 @Override
879 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
880 return mDownloaderBinder;
881 }
882
883
884 @Override
885 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
886 return mUploaderBinder;
887 }
888
889 @Override
890 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
891 return null;
892 }
893
894 @Override
895 public FileDataStorageManager getStorageManager() {
896 return null;
897 }
898
899 @Override
900 public FileOperationsHelper getFileOperationsHelper() {
901 return null;
902 }
903
904 protected ServiceConnection newTransferenceServiceConnection() {
905 return new PreferencesServiceConnection();
906 }
907
908 /** Defines callbacks for service binding, passed to bindService() */
909 private class PreferencesServiceConnection implements ServiceConnection {
910
911 @Override
912 public void onServiceConnected(ComponentName component, IBinder service) {
913
914 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
915 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
916
917 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
918 Log_OC.d(TAG, "Upload service connected");
919 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
920 } else {
921 return;
922 }
923
924 }
925
926 @Override
927 public void onServiceDisconnected(ComponentName component) {
928 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
929 Log_OC.d(TAG, "Download service suddenly disconnected");
930 mDownloaderBinder = null;
931 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
932 Log_OC.d(TAG, "Upload service suddenly disconnected");
933 mUploaderBinder = null;
934 }
935 }
936 };
937 }