05adf9f96296878eb0689d8b44925bc327a7afad
[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 upcomingStorageDir.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 saveStoragePath(resultStorageDir);
628 }
629 }
630
631 public ActionBar getSupportActionBar() {
632 return getDelegate().getSupportActionBar();
633 }
634
635 public void setSupportActionBar(@Nullable Toolbar toolbar) {
636 getDelegate().setSupportActionBar(toolbar);
637 }
638
639 @Override
640 public MenuInflater getMenuInflater() {
641 return getDelegate().getMenuInflater();
642 }
643
644 @Override
645 public void setContentView(@LayoutRes int layoutResID) {
646 getDelegate().setContentView(layoutResID);
647 }
648 @Override
649 public void setContentView(View view) {
650 getDelegate().setContentView(view);
651 }
652 @Override
653 public void setContentView(View view, ViewGroup.LayoutParams params) {
654 getDelegate().setContentView(view, params);
655 }
656
657 @Override
658 public void addContentView(View view, ViewGroup.LayoutParams params) {
659 getDelegate().addContentView(view, params);
660 }
661
662 @Override
663 protected void onPostResume() {
664 super.onPostResume();
665 getDelegate().onPostResume();
666 }
667
668 @Override
669 protected void onTitleChanged(CharSequence title, int color) {
670 super.onTitleChanged(title, color);
671 getDelegate().setTitle(title);
672 }
673
674 @Override
675 public void onConfigurationChanged(Configuration newConfig) {
676 super.onConfigurationChanged(newConfig);
677 getDelegate().onConfigurationChanged(newConfig);
678 }
679
680 @Override
681 protected void onPostCreate(Bundle savedInstanceState) {
682 super.onPostCreate(savedInstanceState);
683 getDelegate().onPostCreate(savedInstanceState);
684 }
685
686 @Override
687 protected void onDestroy() {
688 mDbHandler.close();
689
690 if (mDownloadServiceConnection != null) {
691 unbindService(mDownloadServiceConnection);
692 mDownloadServiceConnection = null;
693 }
694 if (mUploadServiceConnection != null) {
695 unbindService(mUploadServiceConnection);
696 mUploadServiceConnection = null;
697 }
698
699 super.onDestroy();
700 getDelegate().onDestroy();
701 }
702
703 @Override
704 protected void onStop() {
705 super.onStop();
706 getDelegate().onStop();
707 }
708
709 public void invalidateOptionsMenu() {
710 getDelegate().invalidateOptionsMenu();
711 }
712
713 private AppCompatDelegate getDelegate() {
714 if (mDelegate == null) {
715 mDelegate = AppCompatDelegate.create(this, null);
716 }
717 return mDelegate;
718 }
719
720 /**
721 * Create the list of accounts that has been added into the app
722 */
723 @SuppressWarnings("deprecation")
724 private void addAccountsCheckboxPreferences() {
725
726 // Remove accounts in case list is refreshing for avoiding to have
727 // duplicate items
728 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
729 mAccountsPrefCategory.removeAll();
730 }
731
732 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
733 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
734 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
735
736 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
737 // Show create account screen if there isn't any account
738 am.addAccount(MainApp.getAccountType(), null, null, null, this,
739 null,
740 null);
741 }
742 else {
743
744 for (Account a : accounts) {
745 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
746 accountPreference.setKey(a.name);
747 // Handle internationalized domain names
748 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
749 mAccountsPrefCategory.addPreference(accountPreference);
750
751 // Check the current account that is being used
752 if (a.name.equals(currentAccount.name)) {
753 accountPreference.setChecked(true);
754 } else {
755 accountPreference.setChecked(false);
756 }
757
758 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
759 @Override
760 public boolean onPreferenceChange(Preference preference, Object newValue) {
761 String key = preference.getKey();
762 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
763 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
764 for (Account a : accounts) {
765 RadioButtonPreference p =
766 (RadioButtonPreference) findPreference(a.name);
767 if (key.equals(a.name)) {
768 boolean accountChanged = !p.isChecked();
769 p.setChecked(true);
770 AccountUtils.setCurrentOwnCloudAccount(
771 getApplicationContext(),
772 a.name
773 );
774 if (accountChanged) {
775 // restart the main activity
776 Intent i = new Intent(
777 Preferences.this,
778 FileDisplayActivity.class
779 );
780 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
781 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
782 startActivity(i);
783 } else {
784 finish();
785 }
786 } else {
787 p.setChecked(false);
788 }
789 }
790 return (Boolean) newValue;
791 }
792 });
793
794 }
795
796 // Add Create Account preference at the end of account list if
797 // Multiaccount is enabled
798 if (getResources().getBoolean(R.bool.multiaccount_support)) {
799 createAddAccountPreference();
800 }
801
802 }
803 }
804
805 /**
806 * Create the preference for allow adding new accounts
807 */
808 private void createAddAccountPreference() {
809 Preference addAccountPref = new Preference(this);
810 addAccountPref.setKey("add_account");
811 addAccountPref.setTitle(getString(R.string.prefs_add_account));
812 mAccountsPrefCategory.addPreference(addAccountPref);
813
814 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
815 @Override
816 public boolean onPreferenceClick(Preference preference) {
817 AccountManager am = AccountManager.get(getApplicationContext());
818 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
819 null, null);
820 return true;
821 }
822 });
823
824 }
825
826 /**
827 * Load upload path set on preferences
828 */
829 private void loadInstantUploadPath() {
830 SharedPreferences appPrefs =
831 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
832 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
833 mPrefInstantUploadPath.setSummary(mUploadPath);
834 }
835
836 /**
837 * Save storage path
838 */
839 private void saveStoragePath(String newStoragePath) {
840 SharedPreferences appPrefs =
841 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
842 mStoragePath = newStoragePath;
843 MainApp.setStoragePath(mStoragePath);
844 SharedPreferences.Editor editor = appPrefs.edit();
845 editor.putString("storage_path", mStoragePath);
846 editor.commit();
847 mPrefStoragePath.setSummary(mStoragePath);
848 }
849
850 /**
851 * Load storage path set on preferences
852 */
853 private void loadStoragePath() {
854 SharedPreferences appPrefs =
855 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
856 mStoragePath = appPrefs.getString("storage_path", Environment.getExternalStorageDirectory()
857 .getAbsolutePath());
858 mPrefStoragePath.setSummary(mStoragePath);
859 }
860
861 /**
862 * Save the "Instant Upload Path" on preferences
863 */
864 private void saveInstantUploadPathOnPreferences() {
865 SharedPreferences appPrefs =
866 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
867 SharedPreferences.Editor editor = appPrefs.edit();
868 editor.putString("instant_upload_path", mUploadPath);
869 editor.commit();
870 }
871
872 /**
873 * Load upload video path set on preferences
874 */
875 private void loadInstantUploadVideoPath() {
876 mPrefInstantVideoUploadPath.setSummary(MainApp.getStoragePath());
877 }
878
879 /**
880 * Save the "Instant Video Upload Path" on preferences
881 */
882 private void saveInstantUploadVideoPathOnPreferences() {
883 SharedPreferences appPrefs =
884 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
885 SharedPreferences.Editor editor = appPrefs.edit();
886 editor.putString("instant_video_upload_path", mUploadVideoPath);
887 editor.commit();
888 }
889
890 // Methods for ComponetsGetter
891 @Override
892 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
893 return mDownloaderBinder;
894 }
895
896
897 @Override
898 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
899 return mUploaderBinder;
900 }
901
902 @Override
903 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
904 return null;
905 }
906
907 @Override
908 public FileDataStorageManager getStorageManager() {
909 return null;
910 }
911
912 @Override
913 public FileOperationsHelper getFileOperationsHelper() {
914 return null;
915 }
916
917 protected ServiceConnection newTransferenceServiceConnection() {
918 return new PreferencesServiceConnection();
919 }
920
921 /** Defines callbacks for service binding, passed to bindService() */
922 private class PreferencesServiceConnection implements ServiceConnection {
923
924 @Override
925 public void onServiceConnected(ComponentName component, IBinder service) {
926
927 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
928 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
929
930 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
931 Log_OC.d(TAG, "Upload service connected");
932 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
933 } else {
934 return;
935 }
936
937 }
938
939 @Override
940 public void onServiceDisconnected(ComponentName component) {
941 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
942 Log_OC.d(TAG, "Download service suddenly disconnected");
943 mDownloaderBinder = null;
944 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
945 Log_OC.d(TAG, "Upload service suddenly disconnected");
946 mUploaderBinder = null;
947 }
948 }
949 };
950 }