6310532f9171885b9c6a29cba8df0acf9eddba4f
[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
85 /**
86 * An Activity that allows the user to change the application's settings.
87 *
88 * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used
89 * with AppCompat.
90 */
91 public class Preferences extends PreferenceActivity
92 implements AccountManagerCallback<Boolean>, ComponentsGetter {
93
94 private static final String TAG = "OwnCloudPreferences";
95
96 private static final int ACTION_SELECT_UPLOAD_PATH = 1;
97 private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
98
99 private DbHandler mDbHandler;
100 private CheckBoxPreference pCode;
101 private Preference pAboutApp;
102 private AppCompatDelegate mDelegate;
103
104 private PreferenceCategory mAccountsPrefCategory = null;
105 private final Handler mHandler = new Handler();
106 private String mAccountName;
107 private boolean mShowContextMenu = false;
108 private String mUploadPath;
109 private PreferenceCategory mPrefInstantUploadCategory;
110 private Preference mPrefInstantUpload;
111 private Preference mPrefInstantUploadPath;
112 private Preference mPrefInstantUploadPathWiFi;
113 private Preference mPrefInstantVideoUpload;
114 private Preference mPrefInstantVideoUploadPath;
115 private Preference mPrefInstantVideoUploadPathWiFi;
116 private String mUploadVideoPath;
117
118 protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
119 protected FileUploader.FileUploaderBinder mUploaderBinder = null;
120 private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
121 private PreferenceWithLongSummary mPrefStoragePath;
122 private String mStoragePath;
123
124 @SuppressWarnings("deprecation")
125 @Override
126 public void onCreate(Bundle savedInstanceState) {
127 getDelegate().installViewFactory();
128 getDelegate().onCreate(savedInstanceState);
129 super.onCreate(savedInstanceState);
130 mDbHandler = new DbHandler(getBaseContext());
131 addPreferencesFromResource(R.xml.preferences);
132
133 ActionBar actionBar = getSupportActionBar();
134 actionBar.setDisplayHomeAsUpEnabled(true);
135 actionBar.setTitle(R.string.actionbar_settings);
136
137 // For adding content description tag to a title field in the action bar
138 int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
139 View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
140 if (actionBarTitleView != null) { // it's null in Android 2.x
141 getWindow().getDecorView().findViewById(actionBarTitleId).
142 setContentDescription(getString(R.string.actionbar_settings));
143 }
144
145 // Load the accounts category for adding the list of accounts
146 mAccountsPrefCategory = (PreferenceCategory) findPreference("accounts_category");
147
148 ListView listView = getListView();
149 listView.setOnItemLongClickListener(new OnItemLongClickListener() {
150 @Override
151 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
152 ListView listView = (ListView) parent;
153 ListAdapter listAdapter = listView.getAdapter();
154 Object obj = listAdapter.getItem(position);
155
156 if (obj != null && obj instanceof RadioButtonPreference) {
157 mShowContextMenu = true;
158 mAccountName = ((RadioButtonPreference) obj).getKey();
159
160 String[] items = {
161 getResources().getString(R.string.change_password),
162 getResources().getString(R.string.delete_account)
163 };
164 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Preferences.this);
165 View convertView = getLayoutInflater().inflate(R.layout.alert_dialog_list_view, null);
166 alertDialogBuilder.setView(convertView);
167 ListView lv = (ListView) convertView.findViewById(R.id.list);
168 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
169 Preferences.this,R.layout.simple_dialog_list_item,items);
170 lv.setAdapter(adapter);
171
172 //Setup proper inline listener
173 final AlertDialog alertDialog = alertDialogBuilder.create();
174 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
175 @Override
176 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
177 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
178 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
179 for (Account a : accounts) {
180 if (a.name.equals(mAccountName)) {
181 if (position==0) {
182
183 // Change account password
184 Intent updateAccountCredentials = new Intent(Preferences.this, AuthenticatorActivity.class);
185 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, a);
186 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
187 AuthenticatorActivity.ACTION_UPDATE_TOKEN);
188 startActivity(updateAccountCredentials);
189 alertDialog.cancel();
190
191 } else if (position==1) {
192
193 // Remove account
194 am.removeAccount(a, Preferences.this, mHandler);
195 Log_OC.d(TAG, "Remove an account " + a.name);
196 alertDialog.cancel();
197 }
198 }
199 }
200 }
201 });
202 alertDialog.show();
203
204 View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
205 return longListener.onLongClick(view);
206 }
207 return false;
208 }
209 });
210
211 // Load package info
212 String temp;
213 try {
214 PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
215 temp = pkg.versionName;
216 } catch (NameNotFoundException e) {
217 temp = "";
218 Log_OC.e(TAG, "Error while showing about dialog", e);
219 }
220 final String appVersion = temp;
221
222 // Register context menu for list of preferences.
223 registerForContextMenu(getListView());
224
225 pCode = (CheckBoxPreference) findPreference("set_pincode");
226 if (pCode != null){
227 pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
228 @Override
229 public boolean onPreferenceChange(Preference preference, Object newValue) {
230 Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
231 Boolean enable = (Boolean) newValue;
232 i.setAction(
233 enable.booleanValue() ? PassCodeActivity.ACTION_ENABLE :
234 PassCodeActivity.ACTION_DISABLE
235 );
236 startActivity(i);
237
238 return true;
239 }
240 });
241
242 }
243
244 PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
245
246 boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
247 Preference pHelp = findPreference("help");
248 if (pHelp != null ){
249 if (helpEnabled) {
250 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
251 @Override
252 public boolean onPreferenceClick(Preference preference) {
253 String helpWeb =(String) getText(R.string.url_help);
254 if (helpWeb != null && helpWeb.length() > 0) {
255 Uri uriUrl = Uri.parse(helpWeb);
256 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
257 startActivity(intent);
258 }
259 return true;
260 }
261 });
262 } else {
263 preferenceCategory.removePreference(pHelp);
264 }
265
266 }
267
268 if (BuildConfig.DEBUG) {
269 Preference pLog = findPreference("log");
270 if (pLog != null ){
271 pLog.setOnPreferenceClickListener(new OnPreferenceClickListener() {
272 @Override
273 public boolean onPreferenceClick(Preference preference) {
274 Intent loggerIntent = new Intent(getApplicationContext(),
275 LogHistoryActivity.class);
276 startActivity(loggerIntent);
277 return true;
278 }
279 });
280 }
281 }
282
283 boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
284 Preference pRecommend = findPreference("recommend");
285 if (pRecommend != null){
286 if (recommendEnabled) {
287 pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
288 @Override
289 public boolean onPreferenceClick(Preference preference) {
290
291 Intent intent = new Intent(Intent.ACTION_SENDTO);
292 intent.setType("text/plain");
293 intent.setData(Uri.parse(getString(R.string.mail_recommend)));
294 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
295
296 String appName = getString(R.string.app_name);
297 String downloadUrl = getString(R.string.url_app_download);
298 Account currentAccount = AccountUtils.
299 getCurrentOwnCloudAccount(Preferences.this);
300 String username = currentAccount.name.substring(0,
301 currentAccount.name.lastIndexOf('@'));
302
303 String recommendSubject = String.format(getString(R.string.recommend_subject),
304 appName);
305 String recommendText = String.format(getString(R.string.recommend_text),
306 appName, downloadUrl);
307
308 intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
309 intent.putExtra(Intent.EXTRA_TEXT, recommendText);
310 startActivity(intent);
311
312 return(true);
313
314 }
315 });
316 } else {
317 preferenceCategory.removePreference(pRecommend);
318 }
319
320 }
321
322 boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
323 Preference pFeedback = findPreference("feedback");
324 if (pFeedback != null){
325 if (feedbackEnabled) {
326 pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
327 @Override
328 public boolean onPreferenceClick(Preference preference) {
329 String feedbackMail =(String) getText(R.string.mail_feedback);
330 String feedback =(String) getText(R.string.prefs_feedback) + " - android v" + appVersion;
331 Intent intent = new Intent(Intent.ACTION_SENDTO);
332 intent.setType("text/plain");
333 intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
334
335 intent.setData(Uri.parse(feedbackMail));
336 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
337 startActivity(intent);
338
339 return true;
340 }
341 });
342 } else {
343 preferenceCategory.removePreference(pFeedback);
344 }
345
346 }
347
348 boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
349 Preference pImprint = findPreference("imprint");
350 if (pImprint != null) {
351 if (imprintEnabled) {
352 pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
353 @Override
354 public boolean onPreferenceClick(Preference preference) {
355 String imprintWeb = (String) getText(R.string.url_imprint);
356 if (imprintWeb != null && imprintWeb.length() > 0) {
357 Uri uriUrl = Uri.parse(imprintWeb);
358 Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
359 startActivity(intent);
360 }
361 //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
362 return true;
363 }
364 });
365 } else {
366 preferenceCategory.removePreference(pImprint);
367 }
368 }
369
370 mPrefStoragePath = (PreferenceWithLongSummary)findPreference("storage_path");
371 if (mPrefStoragePath != null){
372
373 mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
374 @Override
375 public boolean onPreferenceChange(Preference preference, Object newValue) {
376 MainApp.setStoragePath((String) newValue);
377 return true;
378 }
379 });
380
381 // mPrefStoragePath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
382 // @Override
383 // public boolean onPreferenceClick(Preference preference) {
384 //
385 //// if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
386 //// mUploadPath += OCFile.PATH_SEPARATOR;
387 //// }
388 //// Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
389 //// intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
390 //// startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
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 }
614 }
615
616 public ActionBar getSupportActionBar() {
617 return getDelegate().getSupportActionBar();
618 }
619
620 public void setSupportActionBar(@Nullable Toolbar toolbar) {
621 getDelegate().setSupportActionBar(toolbar);
622 }
623
624 @Override
625 public MenuInflater getMenuInflater() {
626 return getDelegate().getMenuInflater();
627 }
628
629 @Override
630 public void setContentView(@LayoutRes int layoutResID) {
631 getDelegate().setContentView(layoutResID);
632 }
633 @Override
634 public void setContentView(View view) {
635 getDelegate().setContentView(view);
636 }
637 @Override
638 public void setContentView(View view, ViewGroup.LayoutParams params) {
639 getDelegate().setContentView(view, params);
640 }
641
642 @Override
643 public void addContentView(View view, ViewGroup.LayoutParams params) {
644 getDelegate().addContentView(view, params);
645 }
646
647 @Override
648 protected void onPostResume() {
649 super.onPostResume();
650 getDelegate().onPostResume();
651 }
652
653 @Override
654 protected void onTitleChanged(CharSequence title, int color) {
655 super.onTitleChanged(title, color);
656 getDelegate().setTitle(title);
657 }
658
659 @Override
660 public void onConfigurationChanged(Configuration newConfig) {
661 super.onConfigurationChanged(newConfig);
662 getDelegate().onConfigurationChanged(newConfig);
663 }
664
665 @Override
666 protected void onPostCreate(Bundle savedInstanceState) {
667 super.onPostCreate(savedInstanceState);
668 getDelegate().onPostCreate(savedInstanceState);
669 }
670
671 @Override
672 protected void onDestroy() {
673 mDbHandler.close();
674
675 if (mDownloadServiceConnection != null) {
676 unbindService(mDownloadServiceConnection);
677 mDownloadServiceConnection = null;
678 }
679 if (mUploadServiceConnection != null) {
680 unbindService(mUploadServiceConnection);
681 mUploadServiceConnection = null;
682 }
683
684 super.onDestroy();
685 getDelegate().onDestroy();
686 }
687
688 @Override
689 protected void onStop() {
690 super.onStop();
691 getDelegate().onStop();
692 }
693
694 public void invalidateOptionsMenu() {
695 getDelegate().invalidateOptionsMenu();
696 }
697
698 private AppCompatDelegate getDelegate() {
699 if (mDelegate == null) {
700 mDelegate = AppCompatDelegate.create(this, null);
701 }
702 return mDelegate;
703 }
704
705 /**
706 * Create the list of accounts that has been added into the app
707 */
708 @SuppressWarnings("deprecation")
709 private void addAccountsCheckboxPreferences() {
710
711 // Remove accounts in case list is refreshing for avoiding to have
712 // duplicate items
713 if (mAccountsPrefCategory.getPreferenceCount() > 0) {
714 mAccountsPrefCategory.removeAll();
715 }
716
717 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
718 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
719 Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
720
721 if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
722 // Show create account screen if there isn't any account
723 am.addAccount(MainApp.getAccountType(), null, null, null, this,
724 null,
725 null);
726 }
727 else {
728
729 for (Account a : accounts) {
730 RadioButtonPreference accountPreference = new RadioButtonPreference(this);
731 accountPreference.setKey(a.name);
732 // Handle internationalized domain names
733 accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
734 mAccountsPrefCategory.addPreference(accountPreference);
735
736 // Check the current account that is being used
737 if (a.name.equals(currentAccount.name)) {
738 accountPreference.setChecked(true);
739 } else {
740 accountPreference.setChecked(false);
741 }
742
743 accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
744 @Override
745 public boolean onPreferenceChange(Preference preference, Object newValue) {
746 String key = preference.getKey();
747 AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
748 Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
749 for (Account a : accounts) {
750 RadioButtonPreference p =
751 (RadioButtonPreference) findPreference(a.name);
752 if (key.equals(a.name)) {
753 boolean accountChanged = !p.isChecked();
754 p.setChecked(true);
755 AccountUtils.setCurrentOwnCloudAccount(
756 getApplicationContext(),
757 a.name
758 );
759 if (accountChanged) {
760 // restart the main activity
761 Intent i = new Intent(
762 Preferences.this,
763 FileDisplayActivity.class
764 );
765 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
766 i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
767 startActivity(i);
768 } else {
769 finish();
770 }
771 } else {
772 p.setChecked(false);
773 }
774 }
775 return (Boolean) newValue;
776 }
777 });
778
779 }
780
781 // Add Create Account preference at the end of account list if
782 // Multiaccount is enabled
783 if (getResources().getBoolean(R.bool.multiaccount_support)) {
784 createAddAccountPreference();
785 }
786
787 }
788 }
789
790 /**
791 * Create the preference for allow adding new accounts
792 */
793 private void createAddAccountPreference() {
794 Preference addAccountPref = new Preference(this);
795 addAccountPref.setKey("add_account");
796 addAccountPref.setTitle(getString(R.string.prefs_add_account));
797 mAccountsPrefCategory.addPreference(addAccountPref);
798
799 addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
800 @Override
801 public boolean onPreferenceClick(Preference preference) {
802 AccountManager am = AccountManager.get(getApplicationContext());
803 am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this,
804 null, null);
805 return true;
806 }
807 });
808
809 }
810
811 /**
812 * Load upload path set on preferences
813 */
814 private void loadInstantUploadPath() {
815 SharedPreferences appPrefs =
816 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
817 mUploadPath = appPrefs.getString("instant_upload_path", getString(R.string.instant_upload_path));
818 mPrefInstantUploadPath.setSummary(mUploadPath);
819 }
820
821 /**
822 * Load storage path set on preferences
823 */
824 private void loadStoragePath() {
825 SharedPreferences appPrefs =
826 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
827 mStoragePath = appPrefs.getString("storage_path", Environment.getExternalStorageDirectory()
828 .getAbsolutePath());
829 mPrefStoragePath.setSummary(mStoragePath);
830 }
831
832 /**
833 * Save the "Instant Upload Path" on preferences
834 */
835 private void saveInstantUploadPathOnPreferences() {
836 SharedPreferences appPrefs =
837 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
838 SharedPreferences.Editor editor = appPrefs.edit();
839 editor.putString("instant_upload_path", mUploadPath);
840 editor.commit();
841 }
842
843 /**
844 * Load upload video path set on preferences
845 */
846 private void loadInstantUploadVideoPath() {
847 mPrefInstantVideoUploadPath.setSummary(MainApp.getStoragePath());
848 }
849
850 /**
851 * Save the "Instant Video Upload Path" on preferences
852 */
853 private void saveInstantUploadVideoPathOnPreferences() {
854 SharedPreferences appPrefs =
855 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
856 SharedPreferences.Editor editor = appPrefs.edit();
857 editor.putString("instant_video_upload_path", mUploadVideoPath);
858 editor.commit();
859 }
860
861 // Methods for ComponetsGetter
862 @Override
863 public FileDownloader.FileDownloaderBinder getFileDownloaderBinder() {
864 return mDownloaderBinder;
865 }
866
867
868 @Override
869 public FileUploader.FileUploaderBinder getFileUploaderBinder() {
870 return mUploaderBinder;
871 }
872
873 @Override
874 public OperationsService.OperationsServiceBinder getOperationsServiceBinder() {
875 return null;
876 }
877
878 @Override
879 public FileDataStorageManager getStorageManager() {
880 return null;
881 }
882
883 @Override
884 public FileOperationsHelper getFileOperationsHelper() {
885 return null;
886 }
887
888 protected ServiceConnection newTransferenceServiceConnection() {
889 return new PreferencesServiceConnection();
890 }
891
892 /** Defines callbacks for service binding, passed to bindService() */
893 private class PreferencesServiceConnection implements ServiceConnection {
894
895 @Override
896 public void onServiceConnected(ComponentName component, IBinder service) {
897
898 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
899 mDownloaderBinder = (FileDownloader.FileDownloaderBinder) service;
900
901 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
902 Log_OC.d(TAG, "Upload service connected");
903 mUploaderBinder = (FileUploader.FileUploaderBinder) service;
904 } else {
905 return;
906 }
907
908 }
909
910 @Override
911 public void onServiceDisconnected(ComponentName component) {
912 if (component.equals(new ComponentName(Preferences.this, FileDownloader.class))) {
913 Log_OC.d(TAG, "Download service suddenly disconnected");
914 mDownloaderBinder = null;
915 } else if (component.equals(new ComponentName(Preferences.this, FileUploader.class))) {
916 Log_OC.d(TAG, "Upload service suddenly disconnected");
917 mUploaderBinder = null;
918 }
919 }
920 };
921 }