Android drop-down Shortcut Settings panel add shortcut switch flow

Source: Internet
Author: User
Tags metabase

Quickly set the loading process of the quick switch on the panel, including the process of loading and clicking events such as icons, and the main process of creating a shortcut switch (to add a lock screen switch for example). The Android version discussed in this article is 5.1.

    • The loading process of the shortcut switch
Loading of resource modules
    • Metabase Config parameter

Metabase config is some configuration information for the program. When you create a shortcut switch, you need to load the shortcut switch information according to the parameter configuration file CONFIG. Reference source Qstilehost.java Implementation, the main process:

(1) The Qstilehost class calls the Recreatetiles () method in the constructor function;

(2) Loadtilespecs () in the Recreatetiles () method;

(3) Loadtilespecs () method through Mcontext.getresources (). getString (R.string.quick_settings _tiles_default) Load the quick switch spec "Quick_settings_tiles_default" from CONFIG. This configuration item determines the fast setting by default Which switches and the display order of the switches are displayed on the fixed panel;

(4) Next, create the shortcut Switch Createtile () to instantiate each shortcut switch by using the configuration information obtained from the. config file;

(5) Finally, the shortcut switches created are saved in the member variable Mtiles list, and all the shortcut switches can be obtained by calling Qstilehost.gettiles ().

    • Loading of characters and icons

The character resource is stored in the String.xml file under each value folder. The icon uses a vector image, which is saved under the Drawable folder. The loading of characters and icons is performed in the Handleupdatestate () method of each shortcut switch class file.

Each shortcut switch class needs to inherit the Qstile<tstate extends State> class (detailed instructions are discussed in section 2.3). The tstate here is the state of the shortcut switch, which is passed as a parameter to the Handleupdatestate () method. The character and icon resources are assigned to State.label and State.icon in the Handleupdatestate () method.

    • Loading of animations

The animation resource is saved in the Drawable folder with the icon, which is usually turned on and off in pairs. The animation is loaded by instantiating the Animationicon class in each of the shortcut switch classes, and the resource ID will be used as the parameter and then invoked in the Click event Processing.

Phonestatusbar Creating a View

It is mentioned that the shortcut switch is created in the Qstilehost.createtile () method, and all the shortcut switches can be obtained by calling the Gettiles () method, and then the shortcut switch is loaded into the implementation of the Quick Setup Panel. Refer to Phonestatusbar. The implementation of the Makestatusbarview () method, the main complete part:

(1) First initialize the shortcut switch panel Mqspanel,mqspanel empty after the instantiation of Qstilehost saved on the object Qsh;

(2) Each shortcut switch is added to the Mqspanel by calling Mqspanel.settiles (Qsh.gettiles ()). In addition, when the configuration changes, the Phonestatusbar.updateresources () method is called by calling Mqspanel. Updateresources () Update the shortcut switch panel; at Shing, at Phonestatusbar. Hidekeyguard () method by calling Mqspanel. Refreshalltiles () Refreshes the shortcut switch panel.

Handling of user Interactions

The listener response to the user action is handled in each of the shortcut switch classes.

As mentioned earlier, each shortcut switch class needs to inherit the Qstile<tstate extends State> class.

Qstile<tstate extends State> implements listenable is the base class for the shortcut switch, which is inherited to create a shortcut switch. Two abstract methods need to be implemented Handleclick () and Handleupdatestate (). So it can be seen that qstile is responsible for the handling of click events and the management of the shortcut switch status. Message delivery is done through the handler mechanism.

(1) Monitoring response of the action:

1) Handle the Click event in the Handleclick () method, and the open/Close animation is also called here;

2) Some switches need to rewrite the Handlesecondaryclick () method, such as Wi-Fi and Bluetooth switch, in the Handlesecondaryclick () method to open the details page;

3) There are also switches that need to be rewritten handlelongclick (), such as the inverse color and hotspot switch, where you open a dialog box asking if you want to hide;

4) in the Setlistening () method, add callback listener, accept action and register broadcast, etc.

(2) Management of the State:

1) state management is performed through a looper provided by the host. Each shortcut switch updates the status in Handleupdatestate (). The callback affects the state to trigger another status update by calling Refreshstate () through the work of the shortcut switch Looper.

2) There are three status classes, state classes and Booleanstate classes and Signalstate classes that inherit from the class. Need to determine the status of the switch or not the shortcut switch inheritance Qstile<qstile.booleanstate>, including flight mode, anti-color, flashlight, hot spots, positioning, automatic rotation, Bluetooth and screen casting switch, etc., also need to determine the connection Qstile<qstile.signalstate>, such as Wi-Fi and mobile data network switches, and other direct inheritance qstile<qstile.state>.

(3) In addition, Wi-Fi and Bluetooth switches need to override the Supportsdualtargets () method and the Getdetailadapter () method. Because these two switches are drawn on a layout of two switches and need to display the details page.

    • Create a shortcut switch

For example, to add a lock screen shortcut switch.

Increase in resource modules

The discussion in section 2.1 lets you know that you need to modify the CONFIG. xml file, the String.xml file, and add a vector image or add a PNG image. The lock screen switch does not require animations when clicked, so no animated XML files are added

(1) Find "Quick_settings_ Tiles_default" in \frameworks\base\packages\systemui\res\values\config.xml, add lockscreen, use ", Separated

<string name= "Quick_settings_tiles_default" translatable= "false" >

Wifi,bt,inversion,cell,airplane,rotation,flashlight,location,cast,hotspot,lockscreen

</string>

(2) Add in \frameworks\base\packages\systemui\res\values\string.xml:

<string name= "Quick_settings_lockscreen_label" >

"Lockscreen"

</string>

(3) Add in \frameworks\base\packages\systemui\res\values values-zh-rcn\string.xml:

<string name= "Quick_settings_lockscreen_label" > "Lock Screen" </string>

Other languages are added in the corresponding String.xml file under the appropriate values folder.

(4) Add a picture in the \frameworks\base\packages\systemui\res\drawable-hdpi folder ic_qs_locks- Creen.png, you can also add the vector graph XML file under the Drawable folder;

Creating the Lockscreentile Class

Create the Lockscreentile.java in the/frameworks/base/packages/systemui/src/com/android/systemui/qs/tiles directory.

(1) New lock screen shortcut switch class Lockscreentile extends Qstile<qstile.booleanstate> inherit Qstile class;

(2) Newtilestate () method directly returns an instance of Booleanstate;

@Override

Protected Booleanstate newtilestate () {

return new Booleanstate ();

}

(3) Handleclick () method in response to click event Lock screen, call Powermanager.gotosleep () method;

@Override

protected void Handleclick () {

SetEnabled (!mstate.value);

}

private void SetEnabled (Boolean enable) {

if (enable) {

Final PowerManager pmr = (powermanager) mcontext

. Getsystemservice (Context.power_service);

Pmr.gotosleep (Systemclock.uptimemillis ());

}

}

(4) The Handleupdatestate () method sets the status of the lock screen switch, including icons and characters;

@Override

protected void Handleupdatestate (booleanstate state, Object Arg) {

State.icon = Resourceicon.get (R.drawable.ic_qs_lockscreen);

State.visible = true;

State.label = mcontext.getstring (R.string.quick_settings_lockscreen_label);

}

(5) Instantiation of Lockscreentile, in Qstilehost. In the Createtile () method, add:

Private qstile<?> Createtile (String tilespec) {

if (Tilespec.equals ("WiFi"))

return new Wifitile (this);

else if (Tilespec.equals ("BT"))

return new Bluetoothtile (this);

......

Else if (tilespec.equals ("Lockscreen"))

return new Lockscreentile (this);

else if (Tilespec.startswith (Intenttile.prefix))

Return Intenttile.create (this, tilespec);

Else

throw new IllegalArgumentException ("Bad tile spec:" + tilespec);

}

Android drop-down Shortcut Settings panel add shortcut switch flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.