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
.content
.Intent
; 
  21 import android
.content
.SharedPreferences
; 
  22 import android
.content
.pm
.PackageInfo
; 
  23 import android
.content
.pm
.PackageManager
.NameNotFoundException
; 
  24 import android
.net
.Uri
; 
  25 import android
.os
.Bundle
; 
  26 import android
.preference
.CheckBoxPreference
; 
  27 import android
.preference
.Preference
; 
  28 import android
.preference
.Preference
.OnPreferenceChangeListener
; 
  29 import android
.preference
.Preference
.OnPreferenceClickListener
; 
  30 import android
.preference
.PreferenceCategory
; 
  31 import android
.preference
.PreferenceManager
; 
  33 import com
.actionbarsherlock
.app
.ActionBar
; 
  34 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
; 
  35 import com
.actionbarsherlock
.view
.Menu
; 
  36 import com
.actionbarsherlock
.view
.MenuItem
; 
  37 import com
.owncloud
.android
.R
; 
  38 import com
.owncloud
.android
.db
.DbHandler
; 
  39 import com
.owncloud
.android
.utils
.DisplayUtils
; 
  40 import com
.owncloud
.android
.utils
.Log_OC
; 
  44  * An Activity that allows the user to change the application's settings. 
  46  * @author Bartek Przybylski 
  47  * @author David A. Velasco 
  49 public class Preferences 
extends SherlockPreferenceActivity 
{ 
  51     private static final String TAG 
= "OwnCloudPreferences"; 
  52     private DbHandler mDbHandler
; 
  53     private CheckBoxPreference pCode
; 
  54     //private CheckBoxPreference pLogging; 
  55     //private Preference pLoggingHistory; 
  56     private Preference pAboutApp
; 
  59     @SuppressWarnings("deprecation") 
  61     public void onCreate(Bundle savedInstanceState
) { 
  62         super.onCreate(savedInstanceState
); 
  63         mDbHandler 
= new DbHandler(getBaseContext()); 
  64         addPreferencesFromResource(R
.xml
.preferences
); 
  65         //populateAccountList(); 
  66         ActionBar actionBar 
= getSherlock().getActionBar(); 
  67         actionBar
.setIcon(DisplayUtils
.getSeasonalIconId()); 
  68         actionBar
.setDisplayHomeAsUpEnabled(true
); 
  70         Preference p 
= findPreference("manage_account"); 
  72         p
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
  74             public boolean onPreferenceClick(Preference preference
) { 
  75                 Intent i 
= new Intent(getApplicationContext(), AccountSelectActivity
.class); 
  81         pCode 
= (CheckBoxPreference
) findPreference("set_pincode"); 
  83             pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
  85                 public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
  86                     Intent i 
= new Intent(getApplicationContext(), PinCodeActivity
.class); 
  87                     i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences"); 
  88                     i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString()); 
  99         PreferenceCategory preferenceCategory 
= (PreferenceCategory
) findPreference("more"); 
 101         boolean helpEnabled 
= getResources().getBoolean(R
.bool
.help_enabled
); 
 102         Preference pHelp 
=  findPreference("help"); 
 105                 pHelp
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 107                     public boolean onPreferenceClick(Preference preference
) { 
 108                         String helpWeb   
=(String
) getText(R
.string
.url_help
); 
 109                         if (helpWeb 
!= null 
&& helpWeb
.length() > 0) { 
 110                             Uri uriUrl 
= Uri
.parse(helpWeb
); 
 111                             Intent intent 
= new Intent(Intent
.ACTION_VIEW
, uriUrl
); 
 112                             startActivity(intent
); 
 118                 preferenceCategory
.removePreference(pHelp
); 
 124        boolean recommendEnabled 
= getResources().getBoolean(R
.bool
.recommend_enabled
); 
 125        Preference pRecommend 
=  findPreference("recommend"); 
 126         if (pRecommend 
!= null
){ 
 127             if (recommendEnabled
) { 
 128                 pRecommend
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 130                     public boolean onPreferenceClick(Preference preference
) { 
 132                         Intent intent 
= new Intent(Intent
.ACTION_SENDTO
);  
 133                         intent
.setType("text/plain"); 
 134                         //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this); 
 135                         String appName 
= getString(R
.string
.app_name
); 
 136                         //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@'));  
 137                         //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName); 
 138                         String recommendSubject 
= String
.format(getString(R
.string
.recommend_subject
), appName
); 
 139                         intent
.putExtra(Intent
.EXTRA_SUBJECT
, recommendSubject
); 
 140                         //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username); 
 141                         String recommendText 
= String
.format(getString(R
.string
.recommend_text
), getString(R
.string
.app_name
), getString(R
.string
.url_app_download
)); 
 142                         intent
.putExtra(Intent
.EXTRA_TEXT
, recommendText
); 
 144                         intent
.setData(Uri
.parse(getString(R
.string
.mail_recommend
)));  
 145                         intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);  
 146                         startActivity(intent
); 
 154                 preferenceCategory
.removePreference(pRecommend
); 
 159         boolean feedbackEnabled 
= getResources().getBoolean(R
.bool
.feedback_enabled
); 
 160         Preference pFeedback 
=  findPreference("feedback"); 
 161         if (pFeedback 
!= null
){ 
 162             if (feedbackEnabled
) { 
 163                 pFeedback
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 165                     public boolean onPreferenceClick(Preference preference
) { 
 166                         String feedbackMail   
=(String
) getText(R
.string
.mail_feedback
); 
 167                         String feedback   
=(String
) getText(R
.string
.prefs_feedback
); 
 168                         Intent intent 
= new Intent(Intent
.ACTION_SENDTO
);  
 169                         intent
.setType("text/plain"); 
 170                         intent
.putExtra(Intent
.EXTRA_SUBJECT
, feedback
); 
 172                         intent
.setData(Uri
.parse(feedbackMail
));  
 173                         intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);  
 174                         startActivity(intent
); 
 180                 preferenceCategory
.removePreference(pFeedback
); 
 185         boolean imprintEnabled 
= getResources().getBoolean(R
.bool
.imprint_enabled
); 
 186         Preference pImprint 
=  findPreference("imprint"); 
 187         if (pImprint 
!= null
) { 
 188             if (imprintEnabled
) { 
 189                 pImprint
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 191                     public boolean onPreferenceClick(Preference preference
) { 
 192                         String imprintWeb 
= (String
) getText(R
.string
.url_imprint
); 
 193                         if (imprintWeb 
!= null 
&& imprintWeb
.length() > 0) { 
 194                             Uri uriUrl 
= Uri
.parse(imprintWeb
); 
 195                             Intent intent 
= new Intent(Intent
.ACTION_VIEW
, uriUrl
); 
 196                             startActivity(intent
); 
 198                         //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG"); 
 203                 preferenceCategory
.removePreference(pImprint
); 
 208        pAboutApp 
= (Preference
) findPreference("about_app"); 
 209        if (pAboutApp 
!= null
) {  
 210                pAboutApp
.setTitle(String
.format(getString(R
.string
.about_android
), getString(R
.string
.app_name
))); 
 213                    pkg 
= getPackageManager().getPackageInfo(getPackageName(), 0); 
 214                    pAboutApp
.setSummary(String
.format(getString(R
.string
.about_version
), pkg
.versionName
)); 
 215                } catch (NameNotFoundException e
) { 
 216                    Log_OC
.e(TAG
, "Error while showing about dialog", e
); 
 220        /* DISABLED FOR RELEASE UNTIL FIXED  
 221        pLogging = (CheckBoxPreference) findPreference("log_to_file"); 
 222        if (pLogging != null) { 
 223            pLogging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
 225                public boolean onPreferenceChange(Preference preference, Object newValue) { 
 227                    String logpath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log"; 
 229                    if(!pLogging.isChecked()) { 
 230                        Log_OC.d("Debug", "start logging"); 
 231                        Log_OC.v("PATH", logpath); 
 232                        Log_OC.startLogging(logpath); 
 235                        Log_OC.d("Debug", "stop logging"); 
 236                        Log_OC.stopLogging(); 
 243        pLoggingHistory = (Preference) findPreference("log_history"); 
 244        if (pLoggingHistory != null) { 
 245            pLoggingHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
 248             public boolean onPreferenceClick(Preference preference) { 
 249                 Intent intent = new Intent(getApplicationContext(),LogHistoryActivity.class); 
 250                 startActivity(intent); 
 260     protected void onResume() { 
 261         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); 
 262         boolean state 
= appPrefs
.getBoolean("set_pincode", false
); 
 263         pCode
.setChecked(state
); 
 268     public boolean onCreateOptionsMenu(Menu menu
) { 
 269         super.onCreateOptionsMenu(menu
); 
 274     public boolean onMenuItemSelected(int featureId
, MenuItem item
) { 
 275         super.onMenuItemSelected(featureId
, item
); 
 278         switch (item
.getItemId()) { 
 279         case android
.R
.id
.home
: 
 280             intent 
= new Intent(getBaseContext(), FileDisplayActivity
.class); 
 281             intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
); 
 282             startActivity(intent
); 
 285             Log_OC
.w(TAG
, "Unknown menu item triggered"); 
 292     protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) { 
 293         super.onActivityResult(requestCode
, resultCode
, data
); 
 297     protected void onDestroy() {