Add custom settings UI

This commit is contained in:
Torsten Grote 2019-06-18 17:39:46 -03:00
parent 3981d3d8cc
commit b983414295
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
14 changed files with 251 additions and 6 deletions

View file

@ -25,6 +25,10 @@
android:allowBackup="false"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".settings.SettingsActivity"
android:exported="true" />
<activity
android:name="com.stevesoltys.backup.activity.MainActivity"
android:label="@string/app_name">

View file

@ -0,0 +1,52 @@
package com.stevesoltys.backup.settings;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.stevesoltys.backup.R;
import static android.widget.Toast.LENGTH_SHORT;
import static java.util.Objects.requireNonNull;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
if (getResources().getBoolean(R.bool.show_restore_in_settings)) {
menu.findItem(R.id.action_restore).setVisible(true);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
} else if (item.getItemId() == R.id.action_backup) {
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show();
return true;
} else if (item.getItemId() == R.id.action_restore) {
Toast.makeText(this, "Not yet implemented", LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}

View file

@ -0,0 +1,77 @@
package com.stevesoltys.backup.settings;
import android.app.backup.IBackupManager;
import android.content.ContentResolver;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.TwoStatePreference;
import com.stevesoltys.backup.R;
import static android.content.Context.BACKUP_SERVICE;
import static android.os.ServiceManager.getService;
import static android.provider.Settings.Secure.BACKUP_AUTO_RESTORE;
public class SettingsFragment extends PreferenceFragmentCompat {
private final static String TAG = SettingsFragment.class.getSimpleName();
private IBackupManager backupManager;
private TwoStatePreference backup;
private TwoStatePreference autoRestore;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
backupManager = IBackupManager.Stub.asInterface(getService(BACKUP_SERVICE));
backup = (TwoStatePreference) findPreference("backup");
backup.setOnPreferenceChangeListener((preference, newValue) -> {
boolean enabled = (boolean) newValue;
try {
backupManager.setBackupEnabled(enabled);
return true;
} catch (RemoteException e) {
e.printStackTrace();
backup.setChecked(!enabled);
return false;
}
});
autoRestore = (TwoStatePreference) findPreference("auto_restore");
autoRestore.setOnPreferenceChangeListener((preference, newValue) -> {
boolean enabled = (boolean) newValue;
try {
backupManager.setAutoRestore(enabled);
return true;
} catch (RemoteException e) {
Log.e(TAG, "Error communicating with BackupManager", e);
autoRestore.setChecked(!enabled);
return false;
}
});
}
@Override
public void onStart() {
super.onStart();
try {
backup.setChecked(backupManager.isBackupEnabled());
backup.setEnabled(true);
} catch (RemoteException e) {
Log.e(TAG, "Error communicating with BackupManager", e);
backup.setEnabled(false);
}
ContentResolver resolver = requireContext().getContentResolver();
autoRestore.setChecked(Settings.Secure.getInt(resolver, BACKUP_AUTO_RESTORE, 1) == 1);
}
}

View file

@ -4,11 +4,13 @@ import android.app.backup.BackupTransport;
import android.app.backup.RestoreDescription;
import android.app.backup.RestoreSet;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import com.stevesoltys.backup.settings.SettingsActivity;
import com.stevesoltys.backup.transport.component.BackupComponent;
import com.stevesoltys.backup.transport.component.RestoreComponent;
import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupComponent;
@ -27,11 +29,14 @@ public class ConfigurableBackupTransport extends BackupTransport {
private static final String TAG = TRANSPORT_DIRECTORY_NAME;
private final Context context;
private final BackupComponent backupComponent;
private final RestoreComponent restoreComponent;
ConfigurableBackupTransport(Context context) {
this.context = context;
backupComponent = new ContentProviderBackupComponent(context);
restoreComponent = new ContentProviderRestoreComponent(context);
}
@ -57,6 +62,11 @@ public class ConfigurableBackupTransport extends BackupTransport {
return 0;
}
@Override
public Intent dataManagementIntent() {
return new Intent(context, SettingsActivity.class);
}
@Override
public boolean isAppEligibleForBackup(PackageInfo targetPackage, boolean isFullBackup) {
return true;

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/textColorSecondary"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM14,13v4h-4v-4H7l5,-5 5,5h-3z" />
</vector>

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/textColorSecondary"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M11,17h2v-6h-2v6zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM11,9h2L13,7h-2v2z" />
</vector>

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/textColorSecondary"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2,20h20v-4L2,16v4zM4,17h2v2L4,19v-2zM2,4v4h20L22,4L2,4zM6,7L4,7L4,5h2v2zM2,14h20v-4L2,10v4zM4,11h2v2L4,13v-2z" />
</vector>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="com.stevesoltys.backup.settings.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/action_backup"
android:title="@string/settings_backup_now"
app:showAsAction="never" />
<item
android:id="@+id/action_restore"
android:title="@string/restore_backup_button"
android:visible="false"
app:showAsAction="never"
tools:visible="true" />
</menu>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="show_restore_in_settings">true</bool>
</resources>

View file

@ -23,4 +23,13 @@
<string name="loading_packages">Loading packages…</string>
<string name="initializing">Initializing…</string>
<!-- Settings -->
<string name="settings_backup">Backup my data</string>
<string name="settings_backup_location">Backup location</string>
<string name="settings_backup_external_storage">External Storage</string>
<string name="settings_info">All backups are encrypted on your phone. To restore from backup you will need your 12-word recovery code.</string>
<string name="settings_auto_restore_title">Automatic restore</string>
<string name="settings_auto_restore_summary">When reinstalling an app, restore backed up settings and data</string>
<string name="settings_backup_now">Backup now</string>
</resources>

View file

@ -1,8 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
</resources>

View file

@ -0,0 +1,7 @@
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
</resources>

View file

@ -0,0 +1,32 @@
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.SwitchPreferenceCompat
app:icon="@drawable/ic_cloud_upload"
app:key="backup"
app:persistent="false"
app:title="@string/settings_backup" />
<androidx.preference.Preference
app:dependency="backup"
app:icon="@drawable/ic_storage"
app:key="backup_location"
app:selectable="false"
app:summary="@string/settings_backup_external_storage"
app:title="@string/settings_backup_location" />
<androidx.preference.SwitchPreferenceCompat
app:dependency="backup"
app:key="auto_restore"
app:persistent="false"
app:summary="@string/settings_auto_restore_summary"
app:title="@string/settings_auto_restore_title" />
<androidx.preference.Preference
app:allowDividerAbove="true"
app:allowDividerBelow="false"
app:dependency="backup"
app:icon="@drawable/ic_info_outline"
app:selectable="false"
app:summary="@string/settings_info" />
</androidx.preference.PreferenceScreen>