@ -1,16 +1,16 @@
/ *
/ *
* Copyright 2010 - 2018 Eric Kok et al .
* Copyright 2010 - 2018 Eric Kok et al .
*
*
* Transdroid is free software : you can redistribute it and / or modify
* Transdroid is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
* ( at your option ) any later version .
*
*
* Transdroid is distributed in the hope that it will be useful ,
* Transdroid is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
* GNU General Public License for more details .
*
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License
* along with Transdroid . If not , see < http : //www.gnu.org/licenses/>.
* along with Transdroid . If not , see < http : //www.gnu.org/licenses/>.
* /
* /
@ -50,6 +50,8 @@ import org.transdroid.core.service.AppUpdateJob;
import java.io.FileNotFoundException ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
@EActivity
@EActivity
public class SystemSettingsActivity extends PreferenceCompatActivity {
public class SystemSettingsActivity extends PreferenceCompatActivity {
@ -72,6 +74,9 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
return true ;
return true ;
}
}
} ;
} ;
protected static final int ACTIVITY_IMPORT_SETTINGS = 1 ;
protected static final int ACTIVITY_EXPORT_SETTINGS = 2 ;
@Bean
@Bean
protected NavigationHelper navigationHelper ;
protected NavigationHelper navigationHelper ;
@Bean
@Bean
@ -99,7 +104,8 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
private OnClickListener importSettingsFromFile = new OnClickListener ( ) {
private OnClickListener importSettingsFromFile = new OnClickListener ( ) {
@Override
@Override
public void onClick ( DialogInterface dialog , int which ) {
public void onClick ( DialogInterface dialog , int which ) {
if ( ! navigationHelper . checkSettingsReadPermission ( SystemSettingsActivity . this ) )
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT
& & ! navigationHelper . checkSettingsReadPermission ( SystemSettingsActivity . this ) )
return ; // We are requesting permission to access file storage
return ; // We are requesting permission to access file storage
importSettingsFromFile ( ) ;
importSettingsFromFile ( ) ;
}
}
@ -114,7 +120,8 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
private OnClickListener exportSettingsToFile = new OnClickListener ( ) {
private OnClickListener exportSettingsToFile = new OnClickListener ( ) {
@Override
@Override
public void onClick ( DialogInterface dialog , int which ) {
public void onClick ( DialogInterface dialog , int which ) {
if ( ! navigationHelper . checkSettingsWritePermission ( SystemSettingsActivity . this ) )
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT
& & ! navigationHelper . checkSettingsWritePermission ( SystemSettingsActivity . this ) )
return ; // We are requesting permission to access file storage
return ; // We are requesting permission to access file storage
exportSettingsToFile ( ) ;
exportSettingsToFile ( ) ;
}
}
@ -171,10 +178,17 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
}
}
private void importSettingsFromFile ( ) {
private void importSettingsFromFile ( ) {
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
try {
try {
settingsPersistence . importSettingsFromFile ( prefs , SettingsPersistence . DEFAULT_SETTINGS_FILE ) ;
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT ) {
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_import_success ) ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
settingsPersistence . importSettingsFromFile ( prefs , SettingsPersistence . DEFAULT_SETTINGS_FILE ) ;
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_import_success ) ) ;
} else {
Intent intent = new Intent ( Intent . ACTION_OPEN_DOCUMENT ) ;
intent . addCategory ( Intent . CATEGORY_OPENABLE ) ;
intent . setType ( "application/json" ) ;
startActivityForResult ( intent , ACTIVITY_IMPORT_SETTINGS ) ;
}
} catch ( FileNotFoundException e ) {
} catch ( FileNotFoundException e ) {
SnackbarManager
SnackbarManager
. show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_file_not_found ) . colorResource ( R . color . red ) ) ;
. show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_file_not_found ) . colorResource ( R . color . red ) ) ;
@ -184,17 +198,59 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
}
}
}
}
@OnActivityResult ( ACTIVITY_IMPORT_SETTINGS )
public void importSettingsFilePicked ( int resultCode , Intent data ) {
if ( resultCode = = RESULT_OK & & data ! = null & & data . getData ( ) ! = null ) {
try {
InputStream fis = getContentResolver ( ) . openInputStream ( data . getData ( ) ) ;
if ( fis ! = null ) {
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
settingsPersistence . importSettingsFromStream ( prefs , fis ) ;
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_import_success ) ) ;
}
} catch ( IOException | JSONException e ) {
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_file_not_found )
. colorResource ( R . color . red ) ) ;
}
}
}
private void exportSettingsToFile ( ) {
private void exportSettingsToFile ( ) {
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
try {
try {
settingsPersistence . exportSettingsToFile ( prefs , SettingsPersistence . DEFAULT_SETTINGS_FILE ) ;
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT ) {
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_export_success ) ) ;
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
settingsPersistence . exportSettingsToFile ( prefs , SettingsPersistence . DEFAULT_SETTINGS_FILE ) ;
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_export_success ) ) ;
} else {
Intent intent = new Intent ( Intent . ACTION_CREATE_DOCUMENT ) ;
intent . addCategory ( Intent . CATEGORY_OPENABLE ) ;
intent . setType ( "application/json" ) ;
intent . putExtra ( Intent . EXTRA_TITLE , SettingsPersistence . DEFAULT_SETTINGS_FILENAME ) ;
startActivityForResult ( intent , ACTIVITY_EXPORT_SETTINGS ) ;
}
} catch ( JSONException | IOException e ) {
} catch ( JSONException | IOException e ) {
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_cant_write_settings_file )
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_cant_write_settings_file )
. colorResource ( R . color . red ) ) ;
. colorResource ( R . color . red ) ) ;
}
}
}
}
@OnActivityResult ( ACTIVITY_EXPORT_SETTINGS )
public void exportSettingsFilePicked ( int resultCode , Intent data ) {
if ( resultCode = = RESULT_OK & & data ! = null & & data . getData ( ) ! = null ) {
try {
OutputStream fos = getContentResolver ( ) . openOutputStream ( data . getData ( ) ) ;
if ( fos ! = null ) {
SharedPreferences prefs = PreferenceManager . getDefaultSharedPreferences ( SystemSettingsActivity . this ) ;
settingsPersistence . exportSettingsToStream ( prefs , fos ) ;
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . pref_export_success ) ) ;
}
} catch ( IOException | JSONException e ) {
SnackbarManager . show ( Snackbar . with ( SystemSettingsActivity . this ) . text ( R . string . error_cant_write_settings_file )
. colorResource ( R . color . red ) ) ;
}
}
}
@OnActivityResult ( BarcodeHelper . ACTIVITY_BARCODE_QRSETTINGS )
@OnActivityResult ( BarcodeHelper . ACTIVITY_BARCODE_QRSETTINGS )
public void onQrCodeScanned ( @SuppressWarnings ( "UnusedParameters" ) int resultCode , Intent data ) {
public void onQrCodeScanned ( @SuppressWarnings ( "UnusedParameters" ) int resultCode , Intent data ) {
// We should have received Intent extras with the QR-decoded data representing Transdroid settings
// We should have received Intent extras with the QR-decoded data representing Transdroid settings
@ -219,22 +275,24 @@ public class SystemSettingsActivity extends PreferenceCompatActivity {
switch ( id ) {
switch ( id ) {
case DIALOG_IMPORTSETTINGS :
case DIALOG_IMPORTSETTINGS :
// @formatter:off
// @formatter:off
return new AlertDialog . Builder ( this )
return new AlertDialog . Builder ( this )
. setMessage (
. setMessage (
getString (
getString (
R . string . pref_import_dialog ,
Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT
getString ( R . string . app_name ) ,
? R . string . pref_import_dialog : R . string . pref_import_dialog_android10 ,
SettingsPersistence . DEFAULT_SETTINGS_FILE . toString ( ) ) )
getString ( R . string . app_name ) ,
. setPositiveButton ( R . string . pref_import_fromfile , importSettingsFromFile )
SettingsPersistence . DEFAULT_SETTINGS_FILE . toString ( ) ) )
. setNeutralButton ( R . string . pref_import_fromqr , importSettingsFromQr )
. setPositiveButton ( R . string . pref_import_fromfile , importSettingsFromFile )
. setNegativeButton ( android . R . string . cancel , null ) . create ( ) ;
. setNeutralButton ( R . string . pref_import_fromqr , importSettingsFromQr )
// @formatter:on
. setNegativeButton ( android . R . string . cancel , null ) . create ( ) ;
// @formatter:on
case DIALOG_EXPORTSETTINGS :
case DIALOG_EXPORTSETTINGS :
// @formatter:off
// @formatter:off
return new AlertDialog . Builder ( this )
return new AlertDialog . Builder ( this )
. setMessage (
. setMessage (
getString (
getString (
R . string . pref_export_dialog ,
Build . VERSION . SDK_INT < Build . VERSION_CODES . KITKAT
? R . string . pref_export_dialog : R . string . pref_export_dialog_android10 ,
getString ( R . string . app_name ) ,
getString ( R . string . app_name ) ,
SettingsPersistence . DEFAULT_SETTINGS_FILE . toString ( ) ) )
SettingsPersistence . DEFAULT_SETTINGS_FILE . toString ( ) ) )
. setPositiveButton ( R . string . pref_export_tofile , exportSettingsToFile )
. setPositiveButton ( R . string . pref_export_tofile , exportSettingsToFile )