Home | History | Annotate | Download | only in recovery
      1 /*
      2  * Copyright (C) 2011 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 GrouperUI : public ScreenRecoveryUI {
     38   public:
     39     GrouperUI() :
     40         consecutive_power_keys(0) {
     41     }
     42 
     43     virtual KeyAction CheckKey(int key) {
     44         if (IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) {
     45             return TOGGLE;
     46         }
     47         if (key == KEY_POWER) {
     48             ++consecutive_power_keys;
     49             if (consecutive_power_keys >= 7) {
     50                 return REBOOT;
     51             }
     52         } else {
     53             consecutive_power_keys = 0;
     54         }
     55         return ENQUEUE;
     56     }
     57 
     58   private:
     59     int consecutive_power_keys;
     60 };
     61 
     62 
     63 class GrouperDevice : public Device {
     64   public:
     65     GrouperDevice() :
     66         ui(new GrouperUI) {
     67     }
     68 
     69     RecoveryUI* GetUI() { return ui; }
     70 
     71     int HandleMenuKey(int key_code, int visible) {
     72         if (visible) {
     73             switch (key_code) {
     74               case KEY_DOWN:
     75               case KEY_VOLUMEDOWN:
     76                 return kHighlightDown;
     77 
     78               case KEY_UP:
     79               case KEY_VOLUMEUP:
     80                 return kHighlightUp;
     81 
     82               case KEY_POWER:
     83                 return kInvokeItem;
     84             }
     85         }
     86 
     87         return kNoAction;
     88     }
     89 
     90     BuiltinAction InvokeMenuItem(int menu_position) {
     91         switch (menu_position) {
     92           case 0: return REBOOT;
     93           case 1: return APPLY_ADB_SIDELOAD;
     94           case 2: return WIPE_DATA;
     95           case 3: return WIPE_CACHE;
     96           default: return NO_ACTION;
     97         }
     98     }
     99 
    100     const char* const* GetMenuHeaders() { return HEADERS; }
    101     const char* const* GetMenuItems() { return ITEMS; }
    102 
    103   private:
    104     RecoveryUI* ui;
    105 };
    106 
    107 Device* make_device() {
    108     return new GrouperDevice;
    109 }
    110