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 as published by 
   7  *   the Free Software Foundation, either version 2 of the License, or 
   8  *   (at your option) any later version. 
  10  *   This program is distributed in the hope that it will be useful, 
  11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13  *   GNU General Public License for more details. 
  15  *   You should have received a copy of the GNU General Public License 
  16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  19 package com
.owncloud
.android
.ui
.activity
; 
  21 import java
.util
.Vector
; 
  23 import android
.content
.Intent
; 
  24 import android
.content
.SharedPreferences
; 
  25 import android
.content
.pm
.PackageInfo
; 
  26 import android
.content
.pm
.PackageManager
.NameNotFoundException
; 
  27 import android
.os
.Bundle
; 
  28 import android
.preference
.CheckBoxPreference
; 
  29 import android
.preference
.ListPreference
; 
  30 import android
.preference
.Preference
; 
  31 import android
.preference
.Preference
.OnPreferenceChangeListener
; 
  32 import android
.preference
.Preference
.OnPreferenceClickListener
; 
  33 import android
.preference
.PreferenceManager
; 
  34 import android
.util
.Log
; 
  36 import com
.actionbarsherlock
.app
.ActionBar
; 
  37 import com
.actionbarsherlock
.app
.SherlockPreferenceActivity
; 
  38 import com
.actionbarsherlock
.view
.Menu
; 
  39 import com
.actionbarsherlock
.view
.MenuItem
; 
  40 import com
.owncloud
.android
.Log_OC
; 
  41 import com
.owncloud
.android
.OwnCloudSession
; 
  42 import com
.owncloud
.android
.R
; 
  43 import com
.owncloud
.android
.db
.DbHandler
; 
  46  * An Activity that allows the user to change the application's settings. 
  48  * @author Bartek Przybylski 
  51 public class Preferences 
extends SherlockPreferenceActivity 
implements OnPreferenceChangeListener 
{ 
  53     private static final String TAG 
= "OwnCloudPreferences"; 
  54     private final int mNewSession 
= 47; 
  55     private final int mEditSession 
= 48; 
  56     private DbHandler mDbHandler
; 
  57     private Vector
<OwnCloudSession
> mSessions
; 
  58     private ListPreference mTrackingUpdateInterval
; 
  59     private CheckBoxPreference mDeviceTracking
; 
  60     private CheckBoxPreference pCode
; 
  61     private CheckBoxPreference pLogging
; 
  62     private Preference pAboutApp
; 
  63     private int mSelectedMenuItem
; 
  66     @SuppressWarnings("deprecation") 
  68     public void onCreate(Bundle savedInstanceState
) { 
  69         super.onCreate(savedInstanceState
); 
  70         mDbHandler 
= new DbHandler(getBaseContext()); 
  71         mSessions 
= new Vector
<OwnCloudSession
>(); 
  72         addPreferencesFromResource(R
.xml
.preferences
); 
  73         //populateAccountList(); 
  74         ActionBar actionBar 
= getSherlock().getActionBar(); 
  75         actionBar
.setDisplayHomeAsUpEnabled(true
); 
  76         Preference p 
= findPreference("manage_account"); 
  78         p
.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
  80             public boolean onPreferenceClick(Preference preference
) { 
  81                 Intent i 
= new Intent(getApplicationContext(), AccountSelectActivity
.class); 
  87         pCode 
= (CheckBoxPreference
) findPreference("set_pincode"); 
  89             pCode
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
  91                 public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
  92                     Intent i 
= new Intent(getApplicationContext(), PinCodeActivity
.class); 
  93                     i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "preferences"); 
  94                     i
.putExtra(PinCodeActivity
.EXTRA_NEW_STATE
, newValue
.toString()); 
 101        pAboutApp 
= (Preference
) findPreference("about_app"); 
 102        if (pAboutApp 
!= null
) {  
 103                pAboutApp
.setTitle(getString(R
.string
.app_name
)+" "+getString(R
.string
.about_android
)); 
 106                    pkg 
= getPackageManager().getPackageInfo(getPackageName(), 0); 
 107                    pAboutApp
.setSummary(getString(R
.string
.about_version
)+" "+pkg
.versionName
); 
 108                } catch (NameNotFoundException e
) { 
 109                    Log_OC
.e(TAG
, "Error while showing about dialog", e
); 
 112        pLogging 
= (CheckBoxPreference
) findPreference("log_to_file"); 
 114        if (pLogging 
!= null
) { 
 115            pLogging
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 
 117                public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
 119                    String logpath 
= getApplicationContext().getFilesDir().getAbsolutePath(); 
 121                    if(!pLogging
.isChecked()) { 
 122                        Log_OC
.d("Debug", "start logging"); 
 123                        Log_OC
.v("PATH", logpath
); 
 124                        Log_OC
.startLogging(logpath
); 
 127                        Log_OC
.d("Debug", "stop logging"); 
 128                        Log_OC
.stopLogging(); 
 139     protected void onResume() { 
 140         SharedPreferences appPrefs 
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); 
 141         boolean state 
= appPrefs
.getBoolean("set_pincode", false
); 
 142         pCode
.setChecked(state
); 
 147     public boolean onCreateOptionsMenu(Menu menu
) { 
 148         super.onCreateOptionsMenu(menu
); 
 153     public boolean onMenuItemSelected(int featureId
, MenuItem item
) { 
 154         super.onMenuItemSelected(featureId
, item
); 
 157         switch (item
.getItemId()) { 
 158         //case R.id.addSessionItem: 
 160             intent 
= new Intent(this, PreferencesNewSession
.class); 
 161             startActivityForResult(intent
, mNewSession
); 
 163         case R
.id
.SessionContextEdit
: 
 164             intent 
= new Intent(this, PreferencesNewSession
.class); 
 165             intent
.putExtra("sessionId", mSessions
.get(mSelectedMenuItem
) 
 167             intent
.putExtra("sessionName", mSessions
.get(mSelectedMenuItem
) 
 169             intent
.putExtra("sessionURL", mSessions
.get(mSelectedMenuItem
) 
 171             startActivityForResult(intent
, mEditSession
); 
 173         case android
.R
.id
.home
: 
 174             intent 
= new Intent(getBaseContext(), FileDisplayActivity
.class); 
 175             intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
); 
 176             startActivity(intent
); 
 179             Log_OC
.w(TAG
, "Unknown menu item triggered"); 
 186     protected void onActivityResult(int requestCode
, int resultCode
, Intent data
) { 
 187         super.onActivityResult(requestCode
, resultCode
, data
); 
 191     protected void onDestroy() { 
 198      * Updates various summaries after updates. Also starts and stops  
 201     public boolean onPreferenceChange(Preference preference
, Object newValue
) { 
 202         // Update current account summary 
 203         /*if (preference.equals(mAccountList)) { 
 204             mAccountList.setSummary(newValue.toString()); 
 207         // Update tracking interval summary 
 208         else*/ if (preference
.equals(mTrackingUpdateInterval
)) { 
 209             String trackingSummary 
= getResources().getString( 
 210                     R
.string
.prefs_trackmydevice_interval_summary
); 
 211             trackingSummary 
= String
.format(trackingSummary
, 
 212                     newValue
.toString()); 
 213             mTrackingUpdateInterval
.setSummary(trackingSummary
); 
 216         // Start or stop tracking service 
 217         else if (preference
.equals(mDeviceTracking
)) { 
 218             Intent locationServiceIntent 
= new Intent(); 
 219             locationServiceIntent
 
 220                     .setAction("com.owncloud.android.location.LocationLauncher"); 
 221             locationServiceIntent
.putExtra("TRACKING_SETTING", 
 223             sendBroadcast(locationServiceIntent
);