Home>

In the android system, if you long press the power key, a dialog box will pop up by default, allowing you to select "airplane mode", "mute", "shutdown" and other functions. As shown below:

But these functions are not necessary for android-x86 and other terminal products.This article briefly introduces how to customize the shutdown interface.

My goal is to long press the power button, it will shut down,The "Device will be shut down" selection dialog box appears.If you can select "Yes" to shut down, and "No" to return to the system.

According to the android source code customization points,First you need to have a comprehensive understanding of the entire system,Find the code that pops up the original selection box,It's here:

<pre name="code"

The code called in the display dialog is as follows:

runnable mpowerlongpress=new runnable () {
 public void run () {
  mshouldturnoffonkeyup=false;
  performhapticfeedbacklw (null, hapticfeedbackconstants.long_press, false);
  sendclosesystemwindows (system_dialog_reason_global_actions);
  showglobalactionsdialog ();
 }
};

After calling the showglobalactionsdialog method, a dialog box with options such as "airplane mode", "mute", and "shutdown" will be gathered.

Find here,We know what to do! Kill it and replace it with the shutdown code we want,You're done! In this case,Without further ado,Let's hurry to the showgloabalactiondialog method to see where the shutdown part is!

The implementation part of showglobalactionsdialog is here:

frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ globalaction.java

public void showdialog (boolean keyguardshowing, boolean isdeviceprovisioned) {
 mkeyguardshowing=keyguardshowing;
 mdeviceprovisioned=isdeviceprovisioned;
 if (mdialog == null) {
  mstatusbar=(statusbarmanager) mcontext.getsystemservice (context.status_bar_service);
  mdialog=createdialog ();
 }
 preparedialog ();
 mstatusbar.disable (statusbarmanager.disable_expand);
 mdialog.show ();
}

We can clearly see thatA new mdialog is created here, and then prepare and then show it, then this mdialog is the key,See how it was created by createdialog,Still in this file:

/**
 * create the global actions dialog.
 * @return a new dialog.
 * /
private alertdialog createdialog () {
 msilentmodetoggle=new toggleaction (
   r.drawable.ic_lock_silent_mode,   r.drawable.ic_lock_silent_mode_off,   r.string.global_action_toggle_silent_mode,   r.string.global_action_silent_mode_on_status,   r.string.global_action_silent_mode_off_status) {
  void willcreate () {
   //xxx:fixme:switch to ic_lock_vibrate_mode when available
   menablediconresid=(settings.system.getint (mcontext.getcontentresolver (),     settings.system.vibrate_in_silent, 1) == 1)
    ?r.drawable.ic_lock_silent_mode_vibrate
    :r.drawable.ic_lock_silent_mode;
  }
  void ontoggle (boolean on) {
   if (on) {
    maudiomanager.setringermode ((settings.system.getint (mcontext.getcontentresolver (),     settings.system.vibrate_in_silent, 1) == 1)
     ?audiomanager.ringer_mode_vibrate
     :audiomanager.ringer_mode_silent);
   } else {
    maudiomanager.setringermode (audiomanager.ringer_mode_normal);
   }
  }
  public boolean showduringkeyguard () {
   return true;
  }
  public boolean showbeforeprovisioning () {
   return false;
  }
 };
 mairplanemodeon=new toggleaction (
   r.drawable.ic_lock_airplane_mode,   r.drawable.ic_lock_airplane_mode_off,   r.string.global_actions_toggle_airplane_mode,   r.string.global_actions_airplane_mode_on_status,   r.string.global_actions_airplane_mode_off_status) {
  void ontoggle (boolean on) {
   if (boolean.parseboolean (
     systemproperties.get (telephonyproperties.property_inecm_mode))) {
    miswaitingforecmexit=true;
    //launch ecm exit dialog
    intent ecmdialogintent =
      new intent (telephonyintents.action_show_notice_ecm_block_others, null);
    ecmdialogintent.addflags (intent.flag_activity_new_task);
    mcontext.startactivity (ecmdialogintent);
   } else {
    changeairplanemodesystemsetting (on);
   }
  }
  @override
  protected void changestatefrompress (boolean buttonon) {
   //in ecm mode airplane state cannot be changed
   if (! (boolean.parseboolean (
     systemproperties.get (telephonyproperties.property_inecm_mode)))) {
    mstate=buttonon?state.turningon:state.turningoff;
    mairplanestate=mstate;
   }
  }
  public boolean showduringkeyguard () {
   return true;
  }
  public boolean showbeforeprovisioning () {
   return false;
  }
 };
 <span>mitems=lists.newarraylist (
   //silent mode
   msilentmodetoggle,   //next:airplane mode
   mairplanemodeon,   //last:power off
   new singlepressaction (
     com.android.internal.r.drawable.ic_lock_power_off,     r.string.global_action_power_off) {
    </Span><span><u>public void onpress () {
     //shutdown by making sure radio and power are handled accordingly.
     shutdownthread.shutdown (mcontext, true);
    }</u></span><span>
    public boolean showduringkeyguard () {
     return true;
    }
    public boolean showbeforeprovisioning () {
     return true;
    }</span>
   });
 madapter=new myadapter ();
 final alertdialog.builder ab=new alertdialog.builder (mcontext);
 ab.setadapter (madapter, this)
   .setinversebackgroundforced (true)
   .settitle (r.string.global_actions);
 final alertdialog dialog=ab.create ();
 dialog.getwindow (). settype (windowmanager.layoutparams.type_system_dialog);
 if (! mcontext.getresources (). getboolean (
   com.android.internal.r.bool.config_sf_slowblur)) {
  dialog.getwindow (). setflags (windowmanager.layoutparams.flag_blur_behind,    windowmanager.layoutparams.flag_blur_behind);
 }
 dialog.setondismisslistener (this);
 return dialog;
}

See what we found! !! The blue part is the function called on shutdown! !! The second parameter of the shutdown method identifies whether a query dialog pops up.You can choose to require (true) or not (false). Let me be conservative here,Let ’s choose true, in case you accidentally press the shutdown button,Ha ha. . .

That is,As long as we use

shutdownthread.shutdown (mcontext, true);

Replace the previous

showglobalactionsdialog ();

You're done! What are you waiting for! We modify

frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ phonewindowmanager.java

The source code is as follows:

runnable mpowerlongpress=new runnable () {
 public void run () {
  mshouldturnoffonkeyup=false;
  performhapticfeedbacklw (null, hapticfeedbackconstants.long_press, false);
  sendclosesystemwindows (system_dialog_reason_global_actions);
  //showglobalactionsdialog ();
  shutdownthread.shutdown (mcontext, true);
 }
};

Alright, you're done! !!

Is it over?Found that it didn't compile.. .

Details matter! !!

The original shutdownthread.shutdown (mcontext, true) reference package was not added! !! Fortunately, there is gcc. . .

import com.android.internal.app.shutdownthread;

Add this package to

frameworks \ policies \ base \ phone \ com \ android \ internal \ policy \ impl \ phonewindowmanager.java

, Compile again,Pass, yes!

Look at our results:

Do you feel the pleasure and fulfillment of source code customization?

This is just the beginning.The good show is yet to come! !! Haha

  • Previous How to solve multiple decimals in js decimal operation
  • Next Android development get picture crop from camera or album
  • Trends