Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7d0542f2 authored by Doug Zongker's avatar Doug Zongker
Browse files

C++ class for device-specific code

Replace the device-specific functions with a class.  Move some of the
key handling (for log visibility toggling and rebooting) into the UI
class.  Fix up the key handling so there is less crosstalk between the
immediate keys and the queued keys (an increasing annoyance on
button-limited devices).

Change-Id: I8bdea6505da7974631bf3d9ac3ee308f8c0f76e1
parent 7440630c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ endif
LOCAL_MODULE_TAGS := eng

ifeq ($(TARGET_RECOVERY_UI_LIB),)
  LOCAL_SRC_FILES += default_recovery_ui.c
  LOCAL_SRC_FILES += default_device.cpp
else
  LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB)
endif

default_device.cpp

0 → 100644
+92 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <linux/input.h>

#include "common.h"
#include "device.h"
#include "screen_ui.h"

static const char* HEADERS[] = { "Volume up/down to move highlight;",
                                 "enter button to select.",
                                 "",
                                 NULL };

static const char* ITEMS[] =  {"reboot system now",
                               "apply update from external storage",
                               "apply update from cache",
                               "wipe data/factory reset",
                               "wipe cache partition",
                               NULL };

class DefaultUI : public ScreenRecoveryUI {
  public:
    virtual KeyAction CheckKey(int key) {
        if (key == KEY_HOME) {
            return TOGGLE;
        }
        return ENQUEUE;
    }
};

class DefaultDevice : public Device {
  public:
    DefaultDevice() :
        ui(new DefaultUI) {
    }

    RecoveryUI* GetUI() { return ui; }

    int HandleMenuKey(int key, int visible) {
        if (visible) {
            switch (key) {
              case KEY_DOWN:
              case KEY_VOLUMEDOWN:
                return kHighlightDown;

              case KEY_UP:
              case KEY_VOLUMEUP:
                return kHighlightUp;

              case KEY_ENTER:
                return kInvokeItem;
            }
        }

        return kNoAction;
    }

    BuiltinAction InvokeMenuItem(int menu_position) {
        switch (menu_position) {
          case 0: return REBOOT;
          case 1: return APPLY_EXT;
          case 2: return APPLY_CACHE;
          case 3: return WIPE_DATA;
          case 4: return WIPE_CACHE;
          default: return NO_ACTION;
        }
    }

    const char* const* GetMenuHeaders() { return HEADERS; }
    const char* const* GetMenuItems() { return ITEMS; }

  private:
    RecoveryUI* ui;
};

Device* make_device() {
    return new DefaultDevice();
}

default_recovery_ui.c

deleted100644 → 0
+0 −73
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <linux/input.h>

#include "recovery_ui.h"
#include "common.h"

char* MENU_HEADERS[] = { "Android system recovery utility",
                         "",
                         NULL };

char* MENU_ITEMS[] = { "reboot system now",
                       "apply update from external storage",
                       "wipe data/factory reset",
                       "wipe cache partition",
                       "apply update from cache",
                       NULL };

void device_ui_init(UIParameters* ui_parameters) {
}

int device_recovery_start() {
    return 0;
}

int device_toggle_display(volatile char* key_pressed, int key_code) {
    return key_code == KEY_HOME;
}

int device_reboot_now(volatile char* key_pressed, int key_code) {
    return 0;
}

int device_handle_key(int key_code, int visible) {
    if (visible) {
        switch (key_code) {
            case KEY_DOWN:
            case KEY_VOLUMEDOWN:
                return HIGHLIGHT_DOWN;

            case KEY_UP:
            case KEY_VOLUMEUP:
                return HIGHLIGHT_UP;

            case KEY_ENTER:
                return SELECT_ITEM;
        }
    }

    return NO_ACTION;
}

int device_perform_action(int which) {
    return which;
}

int device_wipe_data() {
    return 0;
}

device.h

0 → 100644
+112 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _RECOVERY_DEVICE_H
#define _RECOVERY_DEVICE_H

#include "ui.h"

class Device {
  public:
    virtual ~Device() { }

    // Called to obtain the UI object that should be used to display
    // the recovery user interface for this device.  You should not
    // have called Init() on the UI object already, the caller will do
    // that after this method returns.
    virtual RecoveryUI* GetUI() = 0;

    // Called when recovery starts up (after the UI has been obtained
    // and initialized and after the arguments have been parsed, but
    // before anything else).
    virtual void StartRecovery() { };

    // enum KeyAction { NONE, TOGGLE, REBOOT };

    // // Called in the input thread when a new key (key_code) is
    // // pressed.  *key_pressed is an array of KEY_MAX+1 bytes
    // // indicating which other keys are already pressed.  Return a
    // // KeyAction to indicate action should be taken immediately.
    // // These actions happen when recovery is not waiting for input
    // // (eg, in the midst of installing a package).
    // virtual KeyAction CheckImmediateKeyAction(volatile char* key_pressed, int key_code) = 0;

    // Called from the main thread when recovery is at the main menu
    // and waiting for input, and a key is pressed.  (Note that "at"
    // the main menu does not necessarily mean the menu is visible;
    // recovery will be at the main menu with it invisible after an
    // unsuccessful operation [ie OTA package failure], or if recovery
    // is started with no command.)
    //
    // key is the code of the key just pressed.  (You can call
    // IsKeyPressed() on the RecoveryUI object you returned from GetUI
    // if you want to find out if other keys are held down.)
    //
    // visible is true if the menu is visible.
    //
    // Return one of the defined constants below in order to:
    //
    //   - move the menu highlight (kHighlight{Up,Down})
    //   - invoke the highlighted item (kInvokeItem)
    //   - do nothing (kNoAction)
    //   - invoke a specific action (a menu position: any non-negative number)
    virtual int HandleMenuKey(int key, int visible) = 0;

    enum BuiltinAction { NO_ACTION, REBOOT, APPLY_EXT, APPLY_CACHE,
                         WIPE_DATA, WIPE_CACHE };

    // Perform a recovery action selected from the menu.
    // 'menu_position' will be the item number of the selected menu
    // item, or a non-negative number returned from
    // device_handle_key().  The menu will be hidden when this is
    // called; implementations can call ui_print() to print
    // information to the screen.  If the menu position is one of the
    // builtin actions, you can just return the corresponding enum
    // value.  If it is an action specific to your device, you
    // actually perform it here and return NO_ACTION.
    virtual BuiltinAction InvokeMenuItem(int menu_position) = 0;

    static const int kNoAction = -1;
    static const int kHighlightUp = -2;
    static const int kHighlightDown = -3;
    static const int kInvokeItem = -4;

    // Called when we do a wipe data/factory reset operation (either via a
    // reboot from the main system with the --wipe_data flag, or when the
    // user boots into recovery manually and selects the option from the
    // menu.)  Can perform whatever device-specific wiping actions are
    // needed.  Return 0 on success.  The userdata and cache partitions
    // are erased AFTER this returns (whether it returns success or not).
    virtual int WipeData() { return 0; }

    // Return the headers (an array of strings, one per line,
    // NULL-terminated) for the main menu.  Typically these tell users
    // what to push to move the selection and invoke the selected
    // item.
    virtual const char* const* GetMenuHeaders() = 0;

    // Return the list of menu items (an array of strings,
    // NULL-terminated).  The menu_position passed to InvokeMenuItem
    // will correspond to the indexes into this array.
    virtual const char* const* GetMenuItems() = 0;
};

// The device-specific library must define this function (or the
// default one will be used, if there is no device-specific library).
// It returns the Device object that recovery should use.
Device* make_device();

#endif  // _DEVICE_H
+34 −36
Original line number Diff line number Diff line
@@ -37,9 +37,9 @@
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "roots.h"
#include "recovery_ui.h"
#include "ui.h"
#include "screen_ui.h"
#include "device.h"

static const struct option OPTIONS[] = {
  { "send_intent", required_argument, NULL, 's' },
@@ -401,7 +401,7 @@ copy_sideloaded_package(const char* original_path) {
}

static const char**
prepend_title(const char** headers) {
prepend_title(const char* const* headers) {
    const char* title[] = { "Android system recovery <"
                            EXPAND(RECOVERY_API_VERSION) "e>",
                            "",
@@ -410,7 +410,7 @@ prepend_title(const char** headers) {
    // count the number of lines in our title, plus the
    // caller-provided headers.
    int count = 0;
    const char** p;
    const char* const* p;
    for (p = title; *p; ++p, ++count);
    for (p = headers; *p; ++p, ++count);

@@ -425,7 +425,7 @@ prepend_title(const char** headers) {

static int
get_menu_selection(const char* const * headers, const char* const * items,
                   int menu_only, int initial_selection) {
                   int menu_only, int initial_selection, Device* device) {
    // throw away keys pressed previously, so user doesn't
    // accidentally trigger menu items.
    ui->FlushKeys();
@@ -444,26 +444,26 @@ get_menu_selection(const char* const * headers, const char* const * items,
            } else {
                LOGI("timed out waiting for key input; rebooting.\n");
                ui->EndMenu();
                return ITEM_REBOOT;
                return 0; // XXX fixme
            }
        }

        int action = device_handle_key(key, visible);
        int action = device->HandleMenuKey(key, visible);

        if (action < 0) {
            switch (action) {
                case HIGHLIGHT_UP:
                case Device::kHighlightUp:
                    --selected;
                    selected = ui->SelectMenu(selected);
                    break;
                case HIGHLIGHT_DOWN:
                case Device::kHighlightDown:
                    ++selected;
                    selected = ui->SelectMenu(selected);
                    break;
                case SELECT_ITEM:
                case Device::kInvokeItem:
                    chosen_item = selected;
                    break;
                case NO_ACTION:
                case Device::kNoAction:
                    break;
            }
        } else if (!menu_only) {
@@ -481,7 +481,7 @@ static int compare_string(const void* a, const void* b) {

static int
update_directory(const char* path, const char* unmount_when_done,
                 int* wipe_cache) {
                 int* wipe_cache, Device* device) {
    ensure_path_mounted(path);

    const char* MENU_HEADERS[] = { "Choose a package to install:",
@@ -555,7 +555,7 @@ update_directory(const char* path, const char* unmount_when_done,
    int result;
    int chosen_item = 0;
    do {
        chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
        chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);

        char* item = zips[chosen_item];
        int item_len = strlen(item);
@@ -570,7 +570,7 @@ update_directory(const char* path, const char* unmount_when_done,
            strlcat(new_path, "/", PATH_MAX);
            strlcat(new_path, item, PATH_MAX);
            new_path[strlen(new_path)-1] = '\0';  // truncate the trailing '/'
            result = update_directory(new_path, unmount_when_done, wipe_cache);
            result = update_directory(new_path, unmount_when_done, wipe_cache, device);
            if (result >= 0) break;
        } else {
            // selected a zip file:  attempt to install it, and return
@@ -608,7 +608,7 @@ update_directory(const char* path, const char* unmount_when_done,
}

static void
wipe_data(int confirm) {
wipe_data(int confirm, Device* device) {
    if (confirm) {
        static const char** title_headers = NULL;

@@ -633,54 +633,54 @@ wipe_data(int confirm) {
                                " No",
                                NULL };

        int chosen_item = get_menu_selection(title_headers, items, 1, 0);
        int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
        if (chosen_item != 7) {
            return;
        }
    }

    ui->Print("\n-- Wiping data...\n");
    device_wipe_data();
    device->WipeData();
    erase_volume("/data");
    erase_volume("/cache");
    ui->Print("Data wipe complete.\n");
}

static void
prompt_and_wait() {
    const char** headers = prepend_title((const char**)MENU_HEADERS);
prompt_and_wait(Device* device) {
    const char* const* headers = prepend_title(device->GetMenuHeaders());

    for (;;) {
        finish_recovery(NULL);
        ui->SetProgressType(RecoveryUI::EMPTY);

        int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
        int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);

        // device-specific code may take some action here.  It may
        // return one of the core actions handled in the switch
        // statement below.
        chosen_item = device_perform_action(chosen_item);
        chosen_item = device->InvokeMenuItem(chosen_item);

        int status;
        int wipe_cache;
        switch (chosen_item) {
            case ITEM_REBOOT:
            case Device::REBOOT:
                return;

            case ITEM_WIPE_DATA:
                wipe_data(ui->IsTextVisible());
            case Device::WIPE_DATA:
                wipe_data(ui->IsTextVisible(), device);
                if (!ui->IsTextVisible()) return;
                break;

            case ITEM_WIPE_CACHE:
            case Device::WIPE_CACHE:
                ui->Print("\n-- Wiping cache...\n");
                erase_volume("/cache");
                ui->Print("Cache wipe complete.\n");
                if (!ui->IsTextVisible()) return;
                break;

            case ITEM_APPLY_SDCARD:
                status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache);
            case Device::APPLY_EXT:
                status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
                if (status == INSTALL_SUCCESS && wipe_cache) {
                    ui->Print("\n-- Wiping cache (at package request)...\n");
                    if (erase_volume("/cache")) {
@@ -700,9 +700,10 @@ prompt_and_wait() {
                    }
                }
                break;
            case ITEM_APPLY_CACHE:

            case Device::APPLY_CACHE:
                // Don't unmount cache at the end of this.
                status = update_directory(CACHE_ROOT, NULL, &wipe_cache);
                status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
                if (status == INSTALL_SUCCESS && wipe_cache) {
                    ui->Print("\n-- Wiping cache (at package request)...\n");
                    if (erase_volume("/cache")) {
@@ -722,7 +723,6 @@ prompt_and_wait() {
                    }
                }
                break;

        }
    }
}
@@ -741,10 +741,8 @@ main(int argc, char **argv) {
    freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
    printf("Starting recovery on %s", ctime(&start));

    // TODO: device_* should be a C++ class; init should return the
    // appropriate UI for the device.
    device_ui_init(&ui_parameters);
    ui = new ScreenRecoveryUI();
    Device* device = make_device();
    ui = device->GetUI();

    ui->Init();
    ui->SetBackground(RecoveryUI::INSTALLING);
@@ -771,7 +769,7 @@ main(int argc, char **argv) {
        }
    }

    device_recovery_start();
    device->StartRecovery();

    printf("Command:");
    for (arg = 0; arg < argc; arg++) {
@@ -809,7 +807,7 @@ main(int argc, char **argv) {
        }
        if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
    } else if (wipe_data) {
        if (device_wipe_data()) status = INSTALL_ERROR;
        if (device->WipeData()) status = INSTALL_ERROR;
        if (erase_volume("/data")) status = INSTALL_ERROR;
        if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
        if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
@@ -822,7 +820,7 @@ main(int argc, char **argv) {

    if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
    if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
        prompt_and_wait();
        prompt_and_wait(device);
    }

    // Otherwise, get ready to boot the main system...
Loading