Home | History | Annotate | Download | only in recovery
      1 /*
      2  * Copyright 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <linux/input.h>
     18 #include <sys/stat.h>
     19 #include <errno.h>
     20 #include <string.h>
     21 
     22 #include "common.h"
     23 #include "device.h"
     24 #include "screen_ui.h"
     25 
     26 const char* HEADERS[] = { "Volume up/down to move highlight;",
     27                           "power button to select.",
     28                           "",
     29                           NULL };
     30 
     31 const char* ITEMS[] = { "reboot system now",
     32                         "apply update from ADB",
     33                         "wipe data/factory reset",
     34                         "wipe cache partition",
     35                         NULL };
     36 
     37 class HammerheadUI : public ScreenRecoveryUI
     38 {
     39 public:
     40     HammerheadUI() :
     41         consecutive_power_keys(0) {
     42     }
     43 
     44     virtual KeyAction CheckKey(int key) {
     45         if (IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) {
     46             return TOGGLE;
     47         }
     48         if (key == KEY_POWER) {
     49             ++consecutive_power_keys;
     50             if (consecutive_power_keys >= 7) {
     51                 return REBOOT;
     52             }
     53         } else {
     54             consecutive_power_keys = 0;
     55         }
     56         return ENQUEUE;
     57     }
     58 
     59     void Init() {
     60       install_overlay_offset_x = 0;
     61       install_overlay_offset_y = 0;
     62       ScreenRecoveryUI::Init();
     63     }
     64 
     65 private:
     66     int consecutive_power_keys;
     67 };
     68 
     69 class HammerheadDevice : public Device
     70 {
     71 public:
     72     HammerheadDevice() :
     73         ui(new HammerheadUI) {
     74     }
     75 
     76     RecoveryUI* GetUI() { return ui; }
     77 
     78     int HandleMenuKey(int key_code, int visible) {
     79         if (visible) {
     80             switch (key_code) {
     81             case KEY_DOWN:
     82             case KEY_VOLUMEDOWN:
     83                 return kHighlightDown;
     84 
     85             case KEY_UP:
     86             case KEY_VOLUMEUP:
     87                 return kHighlightUp;
     88 
     89             case KEY_POWER:
     90                 return kInvokeItem;
     91             }
     92         }
     93 
     94         return kNoAction;
     95     }
     96 
     97     BuiltinAction InvokeMenuItem(int menu_position) {
     98         switch (menu_position) {
     99         case 0: return REBOOT;
    100         case 1: return APPLY_ADB_SIDELOAD;
    101         case 2: return WIPE_DATA;
    102         case 3: return WIPE_CACHE;
    103         default: return NO_ACTION;
    104         }
    105     }
    106 
    107     const char* const* GetMenuHeaders() { return HEADERS; }
    108     const char* const* GetMenuItems() { return ITEMS; }
    109 
    110 private:
    111     RecoveryUI* ui;
    112 };
    113 
    114 Device* make_device() {
    115     return new HammerheadDevice;
    116 }
    117