Android system porting and debugging-------> Add a double-click Physical key to turn the flash on and off and sync the flash status to the drop-down menu

Source: Internet
Author: User

Recently, a customer has such a requirement:

1. Add a switch button in the settings---> Accessibility.

If you turn on the switch, when you double-click a physical button, turn on the flash, and then double-click the physical button to turn off the flash.

If the switch is turned off, when you double-click the physical button, do not do any processing.

2. Turn off the flash status and synchronize in the drop-down menu.

As shown in the effect:

When you open the switch that you added above, if you double-click the POC key (the POC key is a physical button that our company has customized), the Flash is turned on and the Flash icon in the drop-down menu is lit, as shown in

If you double-click the POC key (the POC key is a physical button that our company has customized), turn off the flash and put the flash icon in the drop-down menu off, as shown in

=============================================================================================================== =====================

Here's how to implement this feature:

First step: Add a switch button layout

Add a switch in the Packages/apps/settings/res/xml/accessibility_settings.xml file, where the switch position is placed between the "Press power button End call" and "Auto select Screen", the code is as follows

 <switchpreference android:key= "Toggle_power_button_ends_call_prefe Rence "android:title=" @string/accessibility_power_button_ends_call_prerefence_title "Android                :p ersistent= "false"/> <!--added by Ouyang start [2015-11-3]-<switchpreference android:key= "Toggle_camera_button__start_flashlight_preference" android:title= "@string/accessibility_came Ra_button_start_flashlight_preference_title "android:summary=" @string/accessibility_camera_button_start_fla Shlight_preference_subtitle "android:persistent=" false "/> <!--added by Ouyang end [2015-11-3]--&G         T <switchpreference android:key= "toggle_lock_screen_rotation_preference" android:title= "@st Ring/accelerometer_title "android:persistent=" false "/> 


Add the appropriate string in the Packages/apps/settings/res/values-zh-rcn/strings.xml file

    <!--added by Ouyang start [2015-11-3]-    <string name= "Accessibility_camera_button_start_flashlight_ Preference_title ">" double click [Camera key] to open flash "</string>    <string name=" Accessibility_camera_button_start_ flashlight_preference_title_e580 ">" Double-click [PoC key] to open the flash "</string>    <string name=" Accessibility_camera_ Button_start_flashlight_preference_subtitle ">" When this feature is turned on, the camera is temporarily unable to use "</string>    <!--added by Ouyang end [ 2015-11-3]-

Add the appropriate string in the Packages/apps/settings/res/values/strings.xml file

    <!--added by Ouyang start [2015-11-3]-    <string name= "Accessibility_camera_button_start_flashlight_ Preference_title ">" Camera button Twice,open Flash light "</string>    <string name=" Accessibility_ camera_button_start_flashlight_preference_title_e580 ">" POC button Twice,open Flash light "</string>    <string name= "Accessibility_camera_button_start_flashlight_preference_subtitle" > "After open this feature, the Camera can is used "</string>    <!--added by Ouyang end [2015-11-3]-


The second step: the switch button to do a logical processing, in the Packages/apps/settings/src/com/android/settings/accessibility/accessibilitysettings.java file

Define a string constant, which is the key for the added switch, define the button switch constant and the sharedpreences constant, which is used to save the button to open the off State value

     Added by Ouyang start [2015-11-3]     private static final String toggle_camer_button_start_flashlight_preference =< c2/> "Toggle_camera_button__start_flashlight_preference";    Added by Ouyang end [2015-11-3]     
    Added by Ouyang start [2015-11-3] Private switchpreference mtogglecamerabuttonstartflashlightpreference;private Sharedpreferences preferences;private sharedpreferences.editor editor;//added by Ouyang end   


Through the string to find the defined switch, and do some processing, such as from the saved Perference read the switch to open the closed state and display, in the Initializeallpreferences method, add the following code

Added by Ouyang start [2015-11-3] mtogglecamerabuttonstartflashlightpreference = (switchpreferen        CE) findpreference (toggle_camer_button_start_flashlight_preference);            if (Android.os.SystemProperties.isHanbangVersion ()) {LOG.E (TAG, "is Hanbangversion ()");        Mtogglecamerabuttonstartflashlightpreference.setenabled (FALSE);            }else{log.e (TAG, "is not hanbangversion ()");        Mtogglecamerabuttonstartflashlightpreference.setenabled (TRUE); } log.e (TAG, "mtogglecamerabuttonstartflashlightpreference.isebable=" + Mtogglecamerabuttonstartflashlightpreferen        Ce.isenabled ()); if (Android.os.SystemProperties.isDT800Project ()) {Mtogglecamerabuttonstartflashlightpreference.settitle ( R.string.accessibility_camera_button_start_flashlight_preference_title);} else if (Android.os.SystemProperties.isE580Project ()) {Mtogglecamerabuttonstartflashlightpreference.settitle ( R.string.accessibility_camera_button_start_flashlight_preference_title_e580);} Preferences= getactivity (). Getapplicationcontext (). Getsharedpreferences ("Flashlight", CONTEXT.M Ode_multi_process +context.mode_world_readable +CONTEXT.MODE_WORLD_WR        iteable);        Editor=preferences.edit ();        Boolean Flashchecked=preferences.getboolean ("flashchecked", false);        Mtogglecamerabuttonstartflashlightpreference.setchecked (flashchecked);        LOG.E (TAG, "flashchecked=" +flashchecked);  Added by Ouyang end [2015-11-3]


This code is primarily to initialize the switch, and to judge the different items, DT800 project and E580 project load different strings to describe the switch. These methods can be added yourself in the/frameworks/base/core/java/android/os/systemproperties.java file, with the following code:

       public static Boolean Isdt800project () {return systemproperties.get ("Ro.custom.build.version"). Contains ("DT800");       } Public           Static Boolean Ise580project () {return systemproperties.get ("Ro.custom.build.version"). Contains ("E580") ;        }        public static Boolean ishanbangversion () {return systemproperties.get ("Ro.product.model"). Contains ("Hanbang");        }  

In packages/apps/settings/src/com/android/settings/accessibility/ In the Onpreferencetreeclick method in the Accessibilitysettings.java file, click event Handling is added to the switch.

The code is as follows, adding a few lines of code:

else if (mtogglepowerbuttonendscallpreference = = preference) {            Handletogglepowerbuttonendscallpreferenceclick () ;            return true;        }         Added by Ouyang start [2015-11-3]         else if (mtogglecamerabuttonstartflashlightpreference = = preference) {            Handletogglecamerabuttonstartflashlightpreferenceclick ();            return true;        }         Added by Ouyang end [2015-11-3]         else if (mtogglelockscreenrotationpreference = = preference) {            Handlelockscreenrotationpreferenceclick ();            return true;        


where the Handletogglecamerabuttonstartflashlightpreferenceclick () method is self-defined, the code is as follows:

   Added by Ouyang start [2015-11-3]     private void Handletogglecamerabuttonstartflashlightpreferenceclick () {    if (mtogglecamerabuttonstartflashlightpreference.ischecked ()) {            Editor.putboolean ("flashchecked", true);            Editor.commit ();} else{   Editor.putboolean ("flashchecked", false);   Editor.commit ();}    }    


The main function of this method is to save whether the switch is open or closed, and this state value can be read by other processes.

Step three: Define a broadcastreceiver and dynamically register the broadcast. Code files in Frameworks/base/packages/systemui/src/com/android/systemui/qs/tiles/flashlighttile.java

, add a broadcastreceiver to the file

 Added by Ouyang private final broadcastreceiver mflashbroadcastreceiver =new Broadcastreceiver () {Boolean isdoubl    Epress = false;    @Override public void OnReceive (context context, Intent Intent) {String action = intent.getaction ();    LOG.D (TAG, "action=" + action);    LOG.D (TAG, "isdoublepress=" + isdoublepress); LOG.D (TAG, "now time =" + New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss.    SSS "). Format (new Date ())); if (action = = "Com.runbo.camera.key.up") | | (Action = = "Com.runbo.poc.key.up"))    {Handler Checkhandler = new Handler ();    if (!isdoublepress) {isdoublepress = true;    LOG.I (TAG, "<----===============single Press the Camera Key---->");    Runnable checkdoublerunnable = new Runnable () {@Override public void run () {isdoublepress = false;    }    };    Checkhandler.postdelayed (checkdoublerunnable, 500);    } else {isdoublepress = false;    LOG.I (TAG, "<----============double Press the Camera Key---->"); Boolean HandlerreCeiver = false;    Boolean flashchecked=false;    try {Context mycontext= context.createpackagecontext ("Com.android.settings", context.context_ignore_security); Sharedpreferences sharedpreferences = mycontext.getsharedpreferences ("Flashlight", context.mode_world_readable |        context.mode_multi_process); flashchecked = Sharedpreferences.getboolean ("flashchecked", false);}    catch (Exception e) {log.d (TAG, log.getstacktracestring (e));} if (action = = "Com.runbo.camera.key.up") {if (Android.os.SystemProperties.isDT800Project () &&!ANDROID.OS.S    Ystemproperties.ishanbangversion ()) {handlerreceiver = true; }} else if (action = = "Com.runbo.poc.key.up") {if (Android.os.SystemProperties.isE580Project ()) {Handlerreceiv    ER = true;    }} log.d (TAG, "handlerreceiver=" + handlerreceiver);    LOG.D (TAG, "flashchecked=" + flashchecked); if (handlerreceiver && flashchecked) {log.i (TAG, "============mstate.value=" +mstate.value);    if (!mstate.value) {Boolean newstate =!mstate.value;        Mflashlightcontroller.setflashlight (newstate); Refreshstate (newstate?    UserBoolean.USER_TRUE:UserBoolean.USER_FALSE); LOG.I (TAG, "============setflashlight (true); ============");}    Else{boolean newstate =!mstate.value;        Mflashlightcontroller.setflashlight (newstate); Refreshstate (newstate? UserBoolean.USER_TRUE:UserBoolean.USER_FALSE);    LOG.I (TAG, "============setflashlight (false); ============");} }    }    }    }    };


To register in Flashlighttile's construction method, the code is as follows:

  Public Flashlighttile (host host) {        super (host);        Mflashlightcontroller = Host.getflashlightcontroller ();        Mflashlightcontroller.addlistener (this);                Added by Ouyang start        intentfilter pocfilter = new Intentfilter ();        Pocfilter.addaction ("Com.runbo.poc.key.up");        Pocfilter.addaction ("Com.runbo.camera.key.up");        This.mContext.registerReceiver (Mflashbroadcastreceiver, pocfilter);        LOG.D (TAG, "Registerreceiver (Mflashbroadcastreceiver, Pocfilter)");        Added by Ouyang End    }

The Broadcastreceiver receives the broadcast "Com.runbo.poc.key.up" that is sent by pressing the POC key and releasing the broadcast "Com.runbo.camera.key.up" by pressing the camera key, These two broadcasts are custom broadcasts. Then decide whether to click or double-tap, when double-click and then read the value of sharedpreferences to determine if the switch is open, if the switch is open and then determine if the flash is open or closed, if it is open, double-click to turn off the flash. If it is off, turn on the flash. and update the drop-down menu icon and the corresponding status. If the switch is off, do not open the operation to turn off the flash.


====================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

====================================================================================



Copyright NOTICE: This article is Ouyangpeng original article, welcome reprint, reprint please indicate source! Http://blog.csdn.net/ouyang_peng

Android system porting and debugging-------> Add a double-click Physical key to turn the flash on and off and sync the flash status to the drop-down menu

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.