[Android Apps and Games] Wuby986:
Good morning there!!!

i think we did all the job for the backup/restore function, including the receiver for first boot, but since there are too many possibility of test i am going to ask if any of you guys have time/wants to help in test and debug.. so first of all the code..

we will start adding a new entry in the navbar:

- adding it to string array

- adding icon to drawables and use it in NavigationDrawerFragment.java and in MainActivityView.java a,long with other entries.. as usual the position of icon in list must be always the same..

- we will add it's case to the onNavigationItemSelected in MainActivityView.java, for my current avail options i went for case 5

case 5:
showBackupDialog();
break;

- now we create the method we're calling in the MainActivityView.. we are making it to be a dialog, same as for the set theme:

private void showBackupDialog() {
AlertDialog.Builder b = new AlertDialog.Builder(this);
Adapter adapter = new ArrayAdapter<>(this, R.layout.simple_list_item_single_choice, getResources().getStringArray(R.array.backup_items));
b.setTitle(getString(R.string.backup_dialog_title))
.setSingleChoiceItems((ListAdapter) adapter, PreferenceManager.getDefaultSharedPreferences(this).getInt("backup", 0), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
//we do something based on position
if (position == 0) {
backupPreferences();
} else
if (position == 1 ){
//restorePreferences();
RestorePreferences rp = new RestorePreferences(MainViewActivity.this);
rp.restoreSettings();
}
}
})

;
AlertDialog d = b.create();
d.show();

}

as we can see this is going to call 2 different thing when click.. one is a method to be add still to MainActivityView.java other one is a new class (restore one) that one is also being called from the boot receiver.. so now we add in MainActivityView:

private void backupPreferences() {
//we go into the shared_prefs folder and we read the files name's
File dir = new File("/data/data/com.wubydax.romcontrol/shared_prefs");
for (File f : dir.listFiles()) {
if (f.isFile()) {
String name = f.getName();
//if file is defult pref we don't backup as it doesn't go in the bd
if (!name.equals("com.wubydax.romcontrol_preferences.xml")) {
//we remove the xml suffix from filename and we assign it in the loop to the shared prefs
//this way we can use the getAll method of shared prefs for all the existing files in the folder
name = name.replace(".xml", "");
SharedPreferences pref = getSharedPreferences(name, MODE_PRIVATE);
//now we write to the backup file but first we delete the previous so we don't get multiple entry for same one
File file = new File("/sdcard/prefs.txt");
file.delete();
Map<String, ?> allEntries = pref.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
try(
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/sdcard/prefs.txt", true)))) {
out.println(entry.getKey() + ": " + entry.getValue().toString());

}catch (IOException e) {

}
}
}
}
}
finish();
this.overridePendingTransition(0, R.animator.fadeout);
startActivity(new Intent(this, MainViewActivity.class));
this.overridePendingTransition(R.animator.fadein, 0);
}

so now we can create the boot receiver class and the restore one.. so we create:

BootCompleteReceiver.class

public class BootCompleteReceiver extends BroadcastReceiver {
SharedPreferences prefs;
Boolean firstTime;
String TAG = "receiver";

@Override
public void onReceive(Context context, Intent intent) {
prefs = context.getSharedPreferences("first_boot", Context.MODE_PRIVATE);
firstTime = prefs.getBoolean("first_time", true);
if(firstTime) {
Log.d(TAG, "Receiver Received");
RestorePreferences rp = new RestorePreferences(context);
rp.restoreSettings();

}
}
}

and the RestorePreferences.class

public class RestorePreferences {
ContentResolver cr;
Context context;

public RestorePreferences(Context context) {
this.context = context;
cr = context.getContentResolver();
}

public void restoreSettings() {
//we read the file
File file = new File("/sdcard/prefs.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
//for each line of the file we spilt it into before the ":" the key part and after the value
for (String line = br.readLine(); line != null; line = br.readLine()) {
String key = line.substring(0, line.indexOf(":"));
String value = line.substring(line.indexOf(" ") + 1, line.length());
//test for content resolver
String anyString = Settings.System.getString(cr, "brightness_visibility");
if(anyString!=null) {
Log.d("wuby", anyString);
}else{
Log.d("wuby", "can't get a value from db");
}
Toast.makeText(context, key + value, Toast.LENGTH_SHORT).show();
//if value is a bolean we replace the preference value with a value suitable for database
//then we are ready to write into db
if (value.equals("false")) {
Settings.System.putString(cr, String.valueOf(key), "0");
} else if (value.equals("true")) {
Settings.System.putString(cr, String.valueOf(key), "1");
} else
Settings.System.putString(cr, String.valueOf(key), String.valueOf(value));
}
} catch (IOException e) {
}
}

}

one last thing we have to do is add receiver to manifest and the new permission write to external sd so that it will look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wubydax.romcontrol" >
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Transparent" >
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainViewActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

we've added some comments to the code to make it easier to be understand.. i hope it helps.. if you guys find any error or got any question please let know :)

p.s. of course i didn't mentioned to add strings but i guess that will be straight forward for like title of dialog and so on.. you'll get notified where are missing and you can easy add those by pressing "alt + enter" for array i suggest adding manually to strings and not array as it would do if you press alt + enter

EDIT:

We're already working on proving this.. so if you find some problems just let us know..

we already are implementing popups for confirm of backup and restore, make receiver smater, and backing up also other optins that don't stand in preferences (lke special file preferences)