[Android Apps and Games] daxgirl:
Handling different kinds of preferences:

Now we roll with populating our preference fragments. Each preference fragment is unique for your needs, depends on your use, your modded apps and categories. All we do, as mentioned above, is provide you with preferences to communicate with your modded system apps using ContentResolver class.

For each preference I will explain:

Does it write preferences into shared preferences? and if it does...

What kind of object it writes to shared preferences of our app?

What kind of value we then write into SettingsSystem database?

What kind of key it uses and why?

SwitchPreference/CheckboxPreference:

Images:

You MUST provide defaultValue for ANY switch preference you create

It writes boolean (true or false) into the shared preferences

We copy it as integer (1 or 0) into the Settings.System database

It mush have a unique key, none of existent in databse. It must be the same key as you use in your mod for that function. F.e. statusbar_clock_visibility. Clock can be either visible or invisible. So switch preference will serve us good here. Also will checkbox preference. In your mod when you retrieve and integer using ContentResolver you specify the default value (if the key is not found). You have to specify the same default value here. If in smaly it was 0x1,. then in the app it must be android:defaultValue = "true". YOU MUST SPECIFY DEFAULT!

In preference xml inside your empty PreferenceScreen claws, you add this:

<SwitchPreference
android:defaultValue="true"
android:key="clock_visibility"
android:summaryOff="Clock is hidden"
android:summaryOn="Clock is visible"
android:title="Set Clock Visibility" />

<CheckBoxPreference
android:defaultValue="false"
android:key="brightness_visibility"
android:summaryOff="Brightness slider hidden"
android:summaryOn="Brightness slider is visible"
android:title="Notification Brightness Visibility" />

This is all you need. The key will work automatically. The first time the user runs your app it will look for that key in the database, if it finds it, it will copy the value into our app and the switch will be set accordingly, if it does not find it, it will copy the defaultvalue of your preference to the database.

Of course the proper way of using strings for title and summary is by using string resources. In android studio you can create strings resources after you have typed the string. F.e. click on one of the strings, like for title, and on your keyboard press alt+enter. You will be given an option to extract string resource. That is all up to you. If your app is only in english, you don't really need to do that.

ListPreference:

Usually you would create list preference by specifying <ListPreference..../>. We had added some functionality to native android preferences for List and EditText. So we have our own classes that extend those preferences. So when we create a list preference we use our own class, so the preference looks like this:

<com.wubydax.romcontrol.prefs.MyListPreference
android:defaultValue="2"
android:entries="@array/clock_position_entries"
android:entryValues="@array/clock_position_values"
android:key="any_clock_position"
android:title="Status Bar Clock Position" />

Once you open < in studio and start typing com..... it will give you the options of preferences existing in our app. Just choose the one you need and it will create the name for it. Don't worry if you mistype, it will not compile.

List preference in android persists string. That means that it writes object of string type into the shared preferences. You need to create 2 string arrays for each list preference. One for Entries - what is displayed in the dialog as single choice items for user. and One is for entryValues (what is being written into the preferences). You can from your mod read them as integers or strings using content resolver. f.e., if your values are 200, 300, 400, android will persist them as strings. But when you restrieve them from database in your systemui smali mod, f.e., you can call either getInt (to get them as integers) or getString to get them as strings. Of course strings array like bread, milk, cookies cannot be retrieved as integer. But a string 200 can be either.

When you retrieve a default value in your smali mod, you retrieve f.e. 200. so YOU NEED TO SET 200 as defaultValue. Or in the above case, it's 2. YOU MUST SPECIFY THE DEFAULT STRING!

You do not specify summary for this preference, we take care of it in code. like so:

EditTextPreference:

Code:

<com.wubydax.romcontrol.prefs.MyEditTextPreference
android:defaultValue="simpletext"
android:key="carrier_text"
android:title="Set Custom Carrier Text" />

This preference also persists string. It also writes string into the database. You retrieve it only as string. Because you can't control what user types. You don't want your modded systemui to crash because a user inputted "bread" and you are trying to read it as integer.

YOU MUST SPECIFY THE DEFAULT STRING for EditTextPreference.

ColorPickerPreference:

Writes and integer into the sharedpreferences and we retrieve integer into the database.

Color integers are special. I will not go into how and why. I have created a utility helper app for devs for this purpose. The app's main function is to convert hex string to integers or to reverse smali hex string. You can find an apk and explanations how to use it here.

Remember, YOU MUST-MUST-MUST SPECIFY defaultValue for ColorPicker in our app!!! and YOU MUST USE ACTUAL INTEGERS to do so properly. Just trust us on that. It's no biggie. You use our app for hex converter to both create your default smali hex value and to create an integer for this app.

The code for ColorPickerPreference:

<com.wubydax.romcontrol.prefs.ColorPickerPreference
alphaSlider="true"
android:defaultValue="-16777215"
android:key="clock_color"
android:title="Choose Clock Color" />

As you can see this android:defaultValue="-16777215" is an actual integer for color black. You get it by using our app. You put 000000 into the first text field and click the button. The integer that you get is -16777215. We also have color preview available. It's a very useful tool. Use it and you can never go wrong with neither integers nor smali inverse hex values for your default color.

Now note the special attribute called "alphaSlider" in the code. This is a boolean type. By default it's false. Meaning you can create color picker preference with or withour transparency option.

SeekBarPreference - the SLIDER:

Writes integer of the slideer progress into the database. The moment your finger has stopped tracking the bar, an integer is being registered into the sharedpreference and then being retrieved into the databasse.

<com.wubydax.romcontrol.prefs.SeekBarPreference
min="0"
unitsRight="Kb/s"
android:defaultValue="10"
android:key="network_traffic_autohide_threshold"
android:max="100"
android:title="Autohide Threshold" />

YOU MUST SET DEFAULT VALUE!!!! IN INTEGER!

You can specify special values such as unitsRight, like "%" or "Kb/s" and so on. You can specify the min and the max value. You probably better not specify the summary. But you can if you want.

IntentDialogPreference - App Chooser:

<com.wubydax.romcontrol.prefs.IntentDialogPreference
includeSearch="true"
setSeparatorString="\##"
android:key="choosen_app_gear"
android:title="Choose App" />

This is a very special preference kind. It was created by request from [MENTION=1167171]rompnit[/MENTION]. They use intents from database to open apps in certain mods. You will have to ask them on [MENTION=1042140]tdunham[/MENTION] thread how and when they use it. We created this preference from scratch specifically for them. This is a completely custom kind of android preference. It displays a dialog of all your LAUNCHABLE apps (apps that have default launch intent that can be retrieved by using PackageManager method getLaunchIntentForPackage. In other words - all apps that appear in your launcher will appear on the dialog.

What happens when you click on an app? What is being written into preferences is a special kind of string. The srting might look like this: com.android.settings/com.android.settings.Settings or it might look like this com.android.settings##com.android.settings.Settings... this is what you need to create basic intent. You need package name and activity name. What separates them is up to you. You will need to split them in your smali mod into package name and activity name to create intent. We have created 2 special attributes for this preference:

1. setSeparatorString =this is what will separate the package name from the class name. The default (if you don't specify) is "/". Remember that if you use chars that must be escaped in xml, f.e. like hash(#), you need to escfape them by backslash, like so setSeparatorString = "\##".

2. includeSearch - this is a boolean type of attribute. It is false by default. If you specify true, the search field will appear on the dialog above the apps list, allowing your users to search for an app by name inside the list adapter. We also included a list alphabetical indexer. So it's up to you if it's necessary.

Once the app is chosen, the summary for the preference is set to the app name and the icon on the right side will be the app icon.

Your database will contain now the basic component for the intent. What you do with it in your smali is up to you.

DO NOT SET DEFAULT VALUE FOR THIS PREFERENCE!!!

FilePreference:

This is very special kind of preference requested by [MENTION=1042140]tdunham[/MENTION]. What is does it creates and deletes file with a certain name in our home files dir. It is located at /data/data/com.wubydax.romcontrol/files

<com.wubydax.romcontrol.prefs.FilePreference
android:key=[COLOR="red"]"testfile"[/COLOR]
android:summaryOff="File doesn't exist"
android:summaryOn="File exists"
android:title="Test File Preference" />

The reason this is used by some devs, apparently, is when you need to mod smali file, which originates in a class that does not take context as parameter. Without context you cannot call the ContentResolver, so you cannot retrieve from the database. So what you can do instead is create a boolean condition based on existence or non existence of a certain file. File class does not require context. All it needs is to be instantiated as object and be given a string as path. For exampple:

[COLOR="Green"]File[/COLOR] [COLOR="Blue"]file[/COLOR] = new [COLOR="green"]File[/COLOR]("[COLOR="Purple"]/data/data/com.wubydax.romcontrol/testfile[/COLOR]");

means that object file of class File is a file located at /data/data/com.wubydax.romcontrol/ and called "testfile". In java class File you have a method (boolean) which is called "exists". So a condition can be made like this:

if(file.exists){
int i = 1;
} else {
i = 0;
}

So you can create a boolean method that will return 0 or 1 based on existence of the file at certain location.

This is the idea behind this preference.

The key for this preference is THE NAME OF THE FILE.

YOU DO NOT SPECIFY THE DEFAULT!!!

The preference looks like switch preference. But it acts differently. Youc an specify summaryOn and Off as you need. It does not write into database. It just creates and deletes the file.

Script executing PreferenceScreen:

As entioned above you can create PreferenceScreen which will execute shell scripts upon click. Example:

<PreferenceScreen
android:key=[COLOR="Red"]"script#test"[/COLOR]
android:summary="Click see what happens"
android:title="New Preference Screen" />

Note the key format. It has to begin with script#! The part after the hash (#) is the name of the script you wish to execute, in this case the script is test.sh which you can find in the source you pulled inside assets folder..

Where are the scripts??? You put them inside assets folder (see post #2). You name your scripts f.e. test.sh script (has to end with .sh). But you do not add the .sh extension to the string (we do that in code).

Every time your app launches it checks for scripts in the assets folder. It wants to make sure all the scripts ar being copied to our home dir. Inside files folder there we create a folder called scripts. All your scripts will be automatically copied there from assets, given permission 0755 and ready to be executed.

Wgen a user clicks on a Script Executing PreferenceScreen, the app looks if a script with that name exists in our files dir. If it does, it attemts to execute it. I have explained in post #2. We also check just in case that the script is executable. There is native java method to do that. So if you or your user just add a script to the scripts folder in home directory and forgot to make it 0755, we do that for you in java with this method:

if (script.exists()) {
[COLOR="Blue"][B]boolean isChmoded = script.canExecute() ? true : false;
if (!isChmoded) {
script.setExecutable(true);
}[/B][/COLOR]
Command command = new Command(0, pathToScript) {
@Override
public void commandCompleted(int id, int exitcode) {
super.commandCompleted(id, exitcode);
if (exitcode != 0) {
Toast.makeText(c, String.valueOf(exitcode), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(c, "Executed Successfully", Toast.LENGTH_SHORT).show();

}
}
};
try {
RootTools.getShell(true).add(command);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}

Make suer your scripts are of valid shell format, test them separately before you put them in the app. Be careful, all the scripts are being executed under sudo. Means anything you write shall be done. Do not make mistakes in your script. Adding su to your scripts is not necessary. We execute them as root anyway.

So...

Create a shell script and test it on your device

Place it inside the assets folder of your app

Call it f.e. killbill.sh

Create a PreferenceScreen entry and givie it a key android:key = "script#killbill"

Compile the app

Clicking on that preference should execute killbill.sh which will now be foind in /data/data/com.wibydax.romcontrol/files/scripts/killbill.sh and have permissions 0755.

DO NOT SET DEFAULT FOR PreferenceScreen EVER!!!

Intent opening Preference Screen:

<PreferenceScreen
android:key=[COLOR="Red"]"com.wubydax.gearreboot.RebootActivity"[/COLOR]
android:summary="Opens TWSwipe app to help you choose a different swipe activity"
android:title="Reset TWSwipe Action" />

This is a regular preference screen, so it would seem, but it has a special function. Usually, in order to open an intent with PreferenceScreen, you need to specify a whole lot of intent rules, like action, target class, target package and so on. We have made your life easy. If you want to link to an app from your rom control application, all you need to do is to specify the activity you wish to run as a key to this preference.

You are not allowed to use "." in any other preference key. If you use "." the app will read it as possible intent. If it cannot resolve it, it will make it disappear from the list.

This kind of preference is fully automated. Once the app reads the key, it does all the work for you, it sets the icon for the preference as the app icon, it creates a viable intent.

If the user doesn't have an app that you link to installed, the app will never appear in the preferences. So the user can never click on it. Because otherwise it would give FC to the app.

Nested PreferenceScreen:

If you include regular preference screen, you never need to set a key. Preference screen that envelops the items inside of it will always lead to a nested preference screen that has some included preferences. and so on. We have a loop running through your entire preference tree and detecting all your preference screen and differentiating them by their abilities (being script executing, being intents and so on).

Nested Preference Screen would look like this:

[COLOR="Red"]<PreferenceScreen
android:summary="New Preference screen"
android:title="New Preference Screen">[/COLOR] [COLOR="Green"]<-- Start of nested preference screen[/COLOR]
<PreferenceCategory android:title="new category" />
<CheckBoxPreference
android:key="text_checkbox"
android:title="Checkbox" />
<SwitchPreference
android:key="test_switch"
android:title="Switch" />
[COLOR="Red"]</PreferenceScreen>[/COLOR] [COLOR="Green"]<-- End of nested preference screen[/COLOR]

Inside of it you can have more preferences and more preference screens.

PreferenceCategory:

Preference category is an enveloping kinda preference. It is different in color and appearence then the rest. It makes sub-portions of preference screen look separate from each other and easier to identify. F.e.:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
[COLOR="Red"] <PreferenceCategory android:title="Status Bar Mods">[/COLOR] [COLOR="Green"]<-- start of preference category[/COLOR]
<SwitchPreference
android:defaultValue="true"
android:key="clock_visibility"
android:summaryOff="Clock is hidden"
android:summaryOn="Clock is visible"
android:title="Set Clock Visibility" />
<CheckBoxPreference
android:defaultValue="false"
android:key="brightness_visibility"
android:summaryOff="Brightness slider hidden"
android:summaryOn="Brightness slider is visible"
android:title="Notification Brightness Visibility" />
<com.wubydax.romcontrol.prefs.MyListPreference
android:defaultValue="2"
android:entries="@array/clock_position_entries"
android:entryValues="@array/clock_position_values"
android:key="any_clock_position"
android:title="Status Bar Clock Position" />
<com.wubydax.romcontrol.prefs.MyEditTextPreference
android:defaultValue="simpletext"
android:key="carrier_text"
android:title="Set Custom Carrier Text"
/>
<com.wubydax.romcontrol.prefs.ColorPickerPreference
alphaSlider="true"
android:defaultValue="-16777215"
android:key="clock_color"
android:title="Choose Clock Color" />
<PreferenceScreen
android:key="script#test"
android:summary="Click see what happens"
android:title="New Preference Screen" />
<com.wubydax.romcontrol.prefs.SeekBarPreference
min="0"
unitsRight="Kb/s"
android:defaultValue="10"
android:icon="@null"
android:key="network_traffic_autohide_threshold"
android:max="100"
android:title="Autohide Threshold" />
<PreferenceScreen
android:summary="New Preference screen"
android:title="New Preference Screen">
<PreferenceCategory android:title="new category" />
<CheckBoxPreference
android:key="text_checkbox"
android:title="Checkbox" />
<SwitchPreference
android:key="test_switch"
android:title="Switch" />
</PreferenceScreen>
<com.wubydax.romcontrol.prefs.IntentDialogPreference
includeSearch="true"
setSeparatorString="\##"
android:key="choosen_app_gear"
android:title="Choose App" />
<com.wubydax.romcontrol.prefs.FilePreference
android:key="testfile"
android:summaryOff="File doesn't exist"
android:summaryOn="File exists"
android:title="Test File Preference" />
[COLOR="red"]</PreferenceCategory>[/COLOR] [COLOR="Green"]<-- end of PreferenceCategory[/COLOR]

</PreferenceScreen>

This is it for now, more per demand and need. Have fun!​