1 /* ownCloud Android client application 
   2  *   Copyright (C) 2011  Bartek Przybylski 
   3  *   Copyright (C) 2012-2013 ownCloud Inc. 
   5  *   This program is free software: you can redistribute it and/or modify 
   6  *   it under the terms of the GNU General Public License version 2, 
   7  *   as published by the Free Software Foundation. 
   9  *   This program is distributed in the hope that it will be useful, 
  10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  12  *   GNU General Public License for more details. 
  14  *   You should have received a copy of the GNU General Public License 
  15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  18 package com
.owncloud
.android
.ui
.activity
; 
  20 import android
.accounts
.Account
; 
  21 import android
.accounts
.AccountManager
; 
  22 import android
.accounts
.AccountManagerCallback
; 
  23 import android
.accounts
.AccountManagerFuture
; 
  24 import android
.content
.Intent
; 
  25 import android
.content
.SharedPreferences
; 
  26 import android
.content
.pm
.PackageInfo
; 
  27 import android
.content
.pm
.PackageManager
.NameNotFoundException
; 
  28 import android
.net
.Uri
; 
  29 import android
.os
.Bundle
; 
  30 import android
.os
.Handler
; 
  31 import android
.preference
.CheckBoxPreference
; 
  32 import android
.preference
.Preference
; 
  33 import android
.preference
.Preference
.OnPreferenceChangeListener
; 
  34 import android
.preference
.Preference
.OnPreferenceClickListener
; 
  35 import android
.preference
.PreferenceCategory
; 
  36 import android
.preference
.PreferenceManager
; 
  37 import android
.view
.ContextMenu
; 
  38 import android
.view
.ContextMenu
.ContextMenuInfo
; 
  39 import android
.view
.View
; 
  40 import android
.widget
.AdapterView
; 
  41 import android
.widget
.AdapterView
.OnItemLongClickListener
; 
  42 import android
.widget
.ListAdapter
; 
  43 import android
.widget
.ListView
; 
  45 import com
.actionbarsherlock
.app
.ActionBar
; 
  46 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
; 
  47 import com
.actionbarsherlock
.view
.Menu
; 
  48 import com
.actionbarsherlock
.view
.MenuItem
; 
  49 import com
.owncloud
.android
.MainApp
; 
  50 import com
.owncloud
.android
.R
; 
  51 import com
.owncloud
.android
.authentication
.AccountUtils
; 
  52 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
; 
  53 import com
.owncloud
.android
.datamodel
.OCFile
; 
  54 import com
.owncloud
.android
.db
.DbHandler
; 
  55 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  56 import com
.owncloud
.android
.ui
.RadioButtonPreference
; 
  57 import com
.owncloud
.android
.utils
.DisplayUtils
; 
  61  * An Activity that allows the user to change the application's settings. 
  63  * @author Bartek Przybylski 
  64  * @author David A. Velasco 
  66 public class Preferences 
extends SherlockPreferenceActivity 
implements AccountManagerCallback
<Boolean
> { 
  68     private static final String TAG 
= "OwnCloudPreferences"; 
  70     private static final int ACTION_SELECT_UPLOAD_PATH 
= 1; 
  71     private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH 
= 2; 
  73     private DbHandler mDbHandler
; 
  74     private CheckBoxPreference pCode
; 
  75     private Preference pAboutApp
; 
  77     private PreferenceCategory mAccountsPrefCategory 
= null
; 
  78     private final Handler mHandler 
= new Handler(); 
  79     private String mAccountName
; 
  80     private boolean mShowContextMenu 
= false
; 
  81     private String mUploadPath
; 
  82     private Preference mPrefInstantUploadPath
; 
  83     private Preference mPrefInstantVideoUploadPath
; 
  84     private String mUploadVideoPath
; 
  87     @SuppressWarnings("deprecation") 
  89     public void onCreate(Bundle savedInstanceState
) { 
  90         super.onCreate(savedInstanceState
); 
  91         mDbHandler 
= new DbHandler(getBaseContext()); 
  92         addPreferencesFromResource(R
.xml
.preferences
); 
  94         ActionBar actionBar 
= getSherlock().getActionBar(); 
  95         actionBar
.setIcon(DisplayUtils
.getSeasonalIconId()); 
  96         actionBar
.setDisplayHomeAsUpEnabled(true
); 
  97         actionBar
.setTitle(R
.string
.actionbar_settings
); 
  99         // Load the accounts category for adding the list of accounts 
 100         mAccountsPrefCategory 
= (PreferenceCategory
) findPreference("accounts_category"); 
 102         ListView listView 
= getListView(); 
 103         listView
.setOnItemLongClickListener(new OnItemLongClickListener() { 
 105             public boolean onItemLongClick(AdapterView
<?
> parent
, View view
, int position
, long id
) { 
 106                 ListView listView 
= (ListView
) parent
; 
 107                 ListAdapter listAdapter 
= listView
.getAdapter(); 
 108                 Object obj 
= listAdapter
.getItem(position
); 
 110                 if (obj 
!= null 
&& obj 
instanceof RadioButtonPreference
) { 
 111                     mShowContextMenu 
= true
; 
 112                     mAccountName 
= ((RadioButtonPreference
) obj
).getKey(); 
 114                     Preferences
.this.openContextMenu(listView
); 
 116                     View
.OnLongClickListener longListener 
= (View
.OnLongClickListener
) obj
; 
 117                     return longListener
.onLongClick(view
); 
 126             PackageInfo pkg 
= getPackageManager().getPackageInfo(getPackageName(), 0); 
 127             temp 
= pkg
.versionName
; 
 128         } catch (NameNotFoundException e
) { 
 130             Log_OC
.e(TAG
, "Error while showing about dialog", e
); 
 132         final String appVersion 
= temp
; 
 134         // Register context menu for list of preferences. 
 135         registerForContextMenu(getListView()); 
 137         pCode 
= (CheckBoxPreference
) findPreference("set_pincode"); 
 139             pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
 141                 public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
 142                     Intent i 
= new Intent(getApplicationContext(), PinCodeActivity
.class); 
 143                     i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences"); 
 144                     i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString()); 
 153         PreferenceCategory preferenceCategory 
= (PreferenceCategory
) findPreference("more"); 
 155         boolean helpEnabled 
= getResources().getBoolean(R
.bool
.help_enabled
); 
 156         Preference pHelp 
=  findPreference("help"); 
 159                 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 161                     public boolean onPreferenceClick(Preference preference
) { 
 162                         String helpWeb   
=(String
) getText(R
.string
.url_help
); 
 163                         if (helpWeb 
!= null 
&& helpWeb
.length() > 0) { 
 164                             Uri uriUrl 
= Uri
.parse(helpWeb
); 
 165                             Intent intent 
= new Intent(Intent
.ACTION_VIEW
, uriUrl
); 
 166                             startActivity(intent
); 
 172                 preferenceCategory
.removePreference(pHelp
); 
 178        boolean recommendEnabled 
= getResources().getBoolean(R
.bool
.recommend_enabled
); 
 179        Preference pRecommend 
=  findPreference("recommend"); 
 180         if (pRecommend 
!= null
){ 
 181             if (recommendEnabled
) { 
 182                 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 184                     public boolean onPreferenceClick(Preference preference
) { 
 186                         Intent intent 
= new Intent(Intent
.ACTION_SENDTO
);  
 187                         intent
.setType("text/plain"); 
 188                         intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));  
 189                         intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);  
 191                         String appName 
= getString(R
.string
.app_name
); 
 192                         String downloadUrl 
= getString(R
.string
.url_app_download
); 
 193                         Account currentAccount 
= AccountUtils
.getCurrentOwnCloudAccount(Preferences
.this); 
 194                         String username 
= currentAccount
.name
.substring(0, currentAccount
.name
.lastIndexOf('@')); 
 196                         String recommendSubject 
= String
.format(getString(R
.string
.recommend_subject
), appName
); 
 197                         String recommendText 
= String
.format(getString(R
.string
.recommend_text
), appName
, downloadUrl
, username
); 
 199                         intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
); 
 200                         intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
); 
 201                         startActivity(intent
); 
 209                 preferenceCategory
.removePreference(pRecommend
); 
 214         boolean feedbackEnabled 
= getResources().getBoolean(R
.bool
.feedback_enabled
); 
 215         Preference pFeedback 
=  findPreference("feedback"); 
 216         if (pFeedback 
!= null
){ 
 217             if (feedbackEnabled
) { 
 218                 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 220                     public boolean onPreferenceClick(Preference preference
) { 
 221                         String feedbackMail   
=(String
) getText(R
.string
.mail_feedback
); 
 222                         String feedback   
=(String
) getText(R
.string
.prefs_feedback
) + " - android v" + appVersion
; 
 223                         Intent intent 
= new Intent(Intent
.ACTION_SENDTO
);  
 224                         intent
.setType("text/plain"); 
 225                         intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
); 
 227                         intent
.setData(Uri
.parse(feedbackMail
));  
 228                         intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);  
 229                         startActivity(intent
); 
 235                 preferenceCategory
.removePreference(pFeedback
); 
 240         boolean imprintEnabled 
= getResources().getBoolean(R
.bool
.imprint_enabled
); 
 241         Preference pImprint 
=  findPreference("imprint"); 
 242         if (pImprint 
!= null
) { 
 243             if (imprintEnabled
) { 
 244                 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 246                     public boolean onPreferenceClick(Preference preference
) { 
 247                         String imprintWeb 
= (String
) getText(R
.string
.url_imprint
); 
 248                         if (imprintWeb 
!= null 
&& imprintWeb
.length() > 0) { 
 249                             Uri uriUrl 
= Uri
.parse(imprintWeb
); 
 250                             Intent intent 
= new Intent(Intent
.ACTION_VIEW
, uriUrl
); 
 251                             startActivity(intent
); 
 253                         //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG"); 
 258                 preferenceCategory
.removePreference(pImprint
); 
 262         mPrefInstantUploadPath 
=  findPreference("instant_upload_path"); 
 263         if (mPrefInstantUploadPath 
!= null
){ 
 265             mPrefInstantUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 267                     public boolean onPreferenceClick(Preference preference
) { 
 268                         if (!mUploadPath
.endsWith(OCFile
.PATH_SEPARATOR
)) { 
 269                             mUploadPath 
+= OCFile
.PATH_SEPARATOR
; 
 271                         Intent intent 
= new Intent(Preferences
.this, UploadPathActivity
.class); 
 272                         intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadPath
); 
 273                         startActivityForResult(intent
, ACTION_SELECT_UPLOAD_PATH
); 
 279         mPrefInstantVideoUploadPath 
=  findPreference("instant_video_upload_path"); 
 280         if (mPrefInstantVideoUploadPath 
!= null
){ 
 282             mPrefInstantVideoUploadPath
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 284                     public boolean onPreferenceClick(Preference preference
) { 
 285                         if (!mUploadVideoPath
.endsWith(OCFile
.PATH_SEPARATOR
)) { 
 286                             mUploadVideoPath 
+= OCFile
.PATH_SEPARATOR
; 
 288                         Intent intent 
= new Intent(Preferences
.this, UploadPathActivity
.class); 
 289                         intent
.putExtra(UploadPathActivity
.KEY_INSTANT_UPLOAD_PATH
, mUploadVideoPath
); 
 290                         startActivityForResult(intent
, ACTION_SELECT_UPLOAD_VIDEO_PATH
); 
 297        pAboutApp 
= (Preference
) findPreference("about_app"); 
 298        if (pAboutApp 
!= null
) {  
 299                pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
))); 
 300                pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), appVersion
)); 
 303        loadInstantUploadPath(); 
 304        loadInstantUploadVideoPath(); 
 309     protected void onPause() { 
 314     public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenuInfo menuInfo
) { 
 316         // Filter for only showing contextual menu when long press on the 
 318         if (mShowContextMenu
) { 
 319             getMenuInflater().inflate(R
.menu
.account_picker_long_click
, menu
); 
 320             mShowContextMenu 
= false
; 
 322         super.onCreateContextMenu(menu
, v
, menuInfo
); 
 326      * Called when the user clicked on an item into the context menu created at 
 327      * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} for 
 328      * every ownCloud {@link Account} , containing 'secondary actions' for them. 
 333     public boolean onContextItemSelected(android
.view
.MenuItem item
) { 
 334         AccountManager am 
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
); 
 335         Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType()); 
 336         for (Account a 
: accounts
) { 
 337             if (a
.name
.equals(mAccountName
)) { 
 338                 if (item
.getItemId() == R
.id
.change_password
) { 
 340                     // Change account password 
 341                     Intent updateAccountCredentials 
= new Intent(this, AuthenticatorActivity
.class); 
 342                     updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
, a
); 
 343                     updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACTION
, 
 344                             AuthenticatorActivity
.ACTION_UPDATE_TOKEN
); 
 345                     startActivity(updateAccountCredentials
); 
 347                 } else if (item
.getItemId() == R
.id
.delete_account
) { 
 350                     am
.removeAccount(a
, this, mHandler
); 
 359     public void run(AccountManagerFuture
<Boolean
> future
) { 
 360         if (future
.isDone()) { 
 361             Account a 
= AccountUtils
.getCurrentOwnCloudAccount(this); 
 362             String accountName 
= ""; 
 364                 Account
[] accounts 
= AccountManager
.get(this).getAccountsByType(MainApp
.getAccountType()); 
 365                 if (accounts
.length 
!= 0) 
 366                     accountName 
= accounts
[0].name
; 
 367                 AccountUtils
.setCurrentOwnCloudAccount(this, accountName
); 
 369             addAccountsCheckboxPreferences(); 
 374     protected void onResume() { 
 376         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); 
 377         boolean state 
= appPrefs
.getBoolean("set_pincode", false
); 
 378         pCode
.setChecked(state
); 
 380         // Populate the accounts category with the list of accounts 
 381         addAccountsCheckboxPreferences(); 
 385     public boolean onCreateOptionsMenu(Menu menu
) { 
 386         super.onCreateOptionsMenu(menu
); 
 391     public boolean onMenuItemSelected(int featureId
, MenuItem item
) { 
 392         super.onMenuItemSelected(featureId
, item
); 
 395         switch (item
.getItemId()) { 
 396         case android
.R
.id
.home
: 
 397             intent 
= new Intent(getBaseContext(), FileDisplayActivity
.class); 
 398             intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
); 
 399             startActivity(intent
); 
 402             Log_OC
.w(TAG
, "Unknown menu item triggered"); 
 409     protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) { 
 410         super.onActivityResult(requestCode
, resultCode
, data
); 
 412         if (requestCode 
== ACTION_SELECT_UPLOAD_PATH 
&& resultCode 
== RESULT_OK
){ 
 414             OCFile folderToUpload 
= (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
); 
 416             mUploadPath 
= folderToUpload
.getRemotePath(); 
 418             mUploadPath 
= DisplayUtils
.getPathWithoutLastSlash(mUploadPath
); 
 420             // Show the path on summary preference 
 421             mPrefInstantUploadPath
.setSummary(mUploadPath
); 
 423             saveInstantUploadPathOnPreferences(); 
 425         } else if (requestCode 
== ACTION_SELECT_UPLOAD_VIDEO_PATH 
&& resultCode 
== RESULT_OK
){ 
 427             OCFile folderToUploadVideo 
= (OCFile
) data
.getParcelableExtra(UploadPathActivity
.EXTRA_FOLDER
); 
 429             mUploadVideoPath 
= folderToUploadVideo
.getRemotePath(); 
 431             mUploadVideoPath 
= DisplayUtils
.getPathWithoutLastSlash(mUploadVideoPath
); 
 433             // Show the video path on summary preference 
 434             mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
); 
 436             saveInstantUploadVideoPathOnPreferences(); 
 441     protected void onDestroy() { 
 447      * Create the list of accounts that has been added into the app 
 449     @SuppressWarnings("deprecation") 
 450     private void addAccountsCheckboxPreferences() { 
 452         // Remove accounts in case list is refreshing for avoiding to have 
 454         if (mAccountsPrefCategory
.getPreferenceCount() > 0) { 
 455             mAccountsPrefCategory
.removeAll(); 
 458         AccountManager am 
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
); 
 459         Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType()); 
 460         Account currentAccount 
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext()); 
 462         if (am
.getAccountsByType(MainApp
.getAccountType()).length 
== 0) { 
 463             // Show create account screen if there isn't any account 
 464             am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, this, 
 470             for (Account a 
: accounts
) { 
 471                 RadioButtonPreference accountPreference 
= new RadioButtonPreference(this); 
 472                 accountPreference
.setKey(a
.name
); 
 473                 // Handle internationalized domain names 
 474                 accountPreference
.setTitle(DisplayUtils
.convertIdn(a
.name
, false
)); 
 475                 mAccountsPrefCategory
.addPreference(accountPreference
); 
 477                 // Check the current account that is being used 
 478                 if (a
.name
.equals(currentAccount
.name
)) { 
 479                     accountPreference
.setChecked(true
); 
 481                     accountPreference
.setChecked(false
); 
 484                 accountPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
 486                     public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
 487                         String key 
= preference
.getKey(); 
 488                         AccountManager am 
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
); 
 489                         Account accounts
[] = am
.getAccountsByType(MainApp
.getAccountType()); 
 490                         for (Account a 
: accounts
) { 
 491                             RadioButtonPreference p 
= (RadioButtonPreference
) findPreference(a
.name
); 
 492                             if (key
.equals(a
.name
)) { 
 493                                 boolean accountChanged 
= !p
.isChecked();  
 495                                 AccountUtils
.setCurrentOwnCloudAccount( 
 496                                         getApplicationContext(), 
 499                                 if (accountChanged
) { 
 500                                     // restart the main activity 
 501                                     Intent i 
= new Intent( 
 503                                             FileDisplayActivity
.class 
 505                                     i
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
); 
 514                         return (Boolean
) newValue
; 
 520             // Add Create Account preference at the end of account list if 
 521             // Multiaccount is enabled 
 522             if (getResources().getBoolean(R
.bool
.multiaccount_support
)) { 
 523                 createAddAccountPreference(); 
 530      * Create the preference for allow adding new accounts 
 532     private void createAddAccountPreference() { 
 533         Preference addAccountPref 
= new Preference(this); 
 534         addAccountPref
.setKey("add_account"); 
 535         addAccountPref
.setTitle(getString(R
.string
.prefs_add_account
)); 
 536         mAccountsPrefCategory
.addPreference(addAccountPref
); 
 538         addAccountPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 540             public boolean onPreferenceClick(Preference preference
) { 
 541                 AccountManager am 
= AccountManager
.get(getApplicationContext()); 
 542                 am
.addAccount(MainApp
.getAccountType(), null
, null
, null
, Preferences
.this, null
, null
); 
 550      * Load upload path set on preferences 
 552     private void loadInstantUploadPath() { 
 553         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); 
 554         mUploadPath 
= appPrefs
.getString("instant_upload_path", getString(R
.string
.instant_upload_path
)); 
 555         mPrefInstantUploadPath
.setSummary(mUploadPath
); 
 559      * Save the "Instant Upload Path" on preferences 
 561     private void saveInstantUploadPathOnPreferences() { 
 562         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());         
 563         SharedPreferences
.Editor editor 
= appPrefs
.edit(); 
 564         editor
.putString("instant_upload_path", mUploadPath
); 
 569      * Load upload video path set on preferences 
 571     private void loadInstantUploadVideoPath() { 
 572         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); 
 573         mUploadVideoPath 
= appPrefs
.getString("instant_video_upload_path", getString(R
.string
.instant_upload_path
)); 
 574         mPrefInstantVideoUploadPath
.setSummary(mUploadVideoPath
); 
 578      * Save the "Instant Video Upload Path" on preferences 
 580     private void saveInstantUploadVideoPathOnPreferences() { 
 581         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());         
 582         SharedPreferences
.Editor editor 
= appPrefs
.edit(); 
 583         editor
.putString("instant_video_upload_path", mUploadVideoPath
);