Add Custom Toggles to Android 4.2 QuickSettings
Android 4.2 JellyBean brought lots of goodies to the plate. One of them was the most awaited QuickToggles / QuickSettings that let you quickly change power toggles, settings right from the Notifications bar.
If you’re a programmer/developer, ROM maker/developer, you can learn how you can add your custom Toggles to QuickSettings on Android 4.2 Jellybean.
The guide explains step-by-step how to mod your favorite ROMs and add your own Toggles to QuickSettings.
How to Add your Own Custom Toggles to Android 4.2 QuickSettings
All you need is access to Android 4.2 Jellybean Open source codebase, and start some hacking. You can also start by decompiling the SystemUI as follows:
time make -j8 SystemUI
SystemUI source code for AOKP, CM 10.1
Step 1.
Navigate to src/com/android/systemui/statusbar/phone/QuickSettings.java, and you will see two options to create a Toggle:
1. addSystemTiles:380 – Static tiles with useful information.
2. addTemporaryTiles:571 – This type of tile will get removed without activity, for example, the alarm quick setting is a temp tile.
Lets talk about the first option in a bit of detail.
// CpuInfo tile
QuickSettingsTileView cpuInfoTile = (QuickSettingsTileView)
inflater.inflate(R.layout.quick_settings_tile, parent, false);
cpuInfoTile.setContent(R.layout.quick_settings_tile_cpuinfo, inflater);
cpuInfoTile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSettingsActivity(Intent.ACTION_POWER_USAGE_SUMMARY);
}
});
mModel.addCpuInfoTile(cpuInfoTile, new QuickSettingsModel.RefreshCallback() {
@Override
public void refreshView(QuickSettingsTileView view, State state) {
ImageView iv = (ImageView) view.findViewById(R.id.cpuinfo_image);
TextView tva = (TextView) view.findViewById(R.id.cpuinfoa_textview);
TextView tvb = (TextView) view.findViewById(R.id.cpuinfob_textview);
Drawable d = mContext.getResources().getDrawable(R.drawable.ic_settings_performance);
String GOV = fileReadOneLine(GOV_FILE);
String FREQ = fileReadOneLine(SCALE_CUR_FILE);
iv.setImageDrawable(d);
tva.setText(GOV);
tvb.setText(FREQ);
view.setContentDescription(
mContext.getString(R.string.accessibility_quick_settings_cpuinfo, GOV));
}
});
parent.addView(cpuInfoTile);
Step 2.
The second part is updating the Toggle with a Widget view. You can view other definitions in here to update various states.
src/com/android/systemui/statusbar/phone/QuickSettingsModel.java:174
private QuickSettingsTileView mCpuInfoTile;
private RefreshCallback mCpuInfoCallback;
private State mCpuInfoState = new State();
The layout definition resides in res/layout/quick_settings_tile_cpuinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright ... -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/cpuinfo_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"/>
<TextView
style="@style/TextAppearance.QuickSettings.TileView"
android:id="@+id/cpuinfoa_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#287AA9"
android:gravity="center"/>
<TextView
style="@style/TextAppearance.QuickSettings.TileView"
android:id="@+id/cpuinfob_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#287AA9"
android:gravity="center"/>
</LinearLayout>
Set contentDescription in res/values/strings.xml
<string name="accessibility_quick_settings_cpuinfo">CpuInfo <xliff:g id="meminfo" example="CpuInfo">%s</xliff:g>.</string>
That would create a basic Widget in your Toggles. You can always shape it to your needs, but this guide should give you a basic idea for getting started.
Related: How to Compile ROMs
Follow us on Google+, Twitter, Facebook for latest in Android.



