[4.2] [QuickSettings] How to add new toggles to 4.2 Android

lithid-cm

Inactive Recognized Developer
Jul 1, 2010
3,189
2,956
0
Cleveland
This is just going to be a quick rundown on what do to add new toggles to android new QuickSettings system. This will take place in SystemUI.

Custom QuickSettings Toggles


Path: frameworks/base/packages/SystemUI
Files:
src/com/android/systemui/statusbar/phone/QuickSettings.java
There are two options when creating a toggle.
addSystemTiles:380 - Static tiles with useful information.
addTemporaryTiles:571 - This type of tile will get removed without activity, for example, the alarm quick setting is a temp tile.

I used SystemTiles
Code:
        // 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);
src/com/android/systemui/statusbar/phone/QuickSettingsModel.java:174
This is needed for the widget inside the toggle to get updated. You can view other definitions in here to update various states.
Code:
    private QuickSettingsTileView mCpuInfoTile;
    private RefreshCallback mCpuInfoCallback;
    private State mCpuInfoState = new State();
res/layout/quick_settings_tile_cpuinfo.xml
This is where we create the layout of the tile and call it from java.
Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<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>
res/values/strings.xml
Needed to setContentDescription()
Code:
    <string name="accessibility_quick_settings_cpuinfo">CpuInfo <xliff:g id="meminfo" example="CpuInfo">%s</xliff:g>.</string>
I know this is a very general overview, but the actuality is there is so much that can be done with this, it would be hard to go into extreme detail. Would be better to just simply leave that stuff for questions in this thread.

If you want to know more, please just ask. Lets make android OURS!​
 

mg2195

Senior Member
Dec 5, 2011
2,983
2,572
0
26
RSM
Will this work with CM10 4.1.2?

No, 4.2 only, as stated by OP

Sent from my Galaxy Nexus using Tapatalk 2
I'd assume he process would be very similar as cm is based off aosp...

Anyways I'm currently downloading the 4.2 aosp, gonna be making (trying) my own rom and will definitely be using this guide in the near future after Im sure I can get everything built. Thanks for the guide, if I'm successfully with what I plan to do, you will definitely be linked and credited.

Sent from my SGH-I997 using Tapatalk 2
 
  • Like
Reactions: GoSooners345

lithid-cm

Inactive Recognized Developer
Jul 1, 2010
3,189
2,956
0
Cleveland
I'd assume he process would be very similar as cm is based off aosp...

Anyways I'm currently downloading the 4.2 aosp, gonna be making (trying) my own rom and will definitely be using this guide in the near future after Im sure I can get everything built. Thanks for the guide, if I'm successfully with what I plan to do, you will definitely be linked and credited.

Sent from my SGH-I997 using Tapatalk 2
I was thinking about putting myself in front of a rom recently. If you are at all interested in teaming up for a rom project, PM. You seem legit and wouldn't mind it one bit.
 

Roladriz

Senior Member
Apr 5, 2010
112
33
0
This is badass!! Thank you so much lithid!!!!

EDIT: Question, What are the numbers representing?

addSystemTiles:380
addTemporaryTiles:571
I assumed they were line numbers but when I checked source it didn't look like anything was referring to addSystemTiles or addTemporaryTiles on those line numbers.

 
Last edited:
  • Like
Reactions: lithid-cm