Резервное копирование / восстановление net.rim.device.api.util.Persitable объекта с DesktopManger - PullRequest
0 голосов
/ 30 октября 2011

Задача - сделать резервную копию / восстановить персистентный объект с помощью BB Desktop Manager или любым другим способом. Основная цель - хранить данные между обновлениями прошивки устройства ...

У меня есть:

public final class UserList implements Persistable {
//The persistable objects.
private Hashtable fData;

//Initialize the class with empty values.
public UserList() {
    fData = new Hashtable();
}

//Initialize the class with the specified values.
public UserList(Hashtable p) {
    fData = p;
}

public Hashtable getData() {
    return fData;
}}

Я также реализовал SyncItem (как показано в одном из примеров)

public final class UserListSync extends SyncItem {

private static UserList fList;

private static final int FIELDTAG_NAME = 1;
private static final int FIELDTAG_AGE = 2;

private static PersistentObject store;

static {
    store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}

public UserListSync() {
}

public String getSyncName() {
    return "Sync Item Sample";
}

public String getSyncName(Locale locale) {
    return null;
}

public int getSyncVersion() {
    return 1;
}

public boolean getSyncData(DataBuffer db, int version) {
    boolean retVal = true;

    synchronized (store) {
        if (store.getContents() != null) {
            fList = (UserList)store.getContents();              
        }
    }
    try {
        Enumeration e = fList.getData().keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = (String) fList.getData().get(key); 
            //Write the name.
            db.writeShort(key.length() + 1);
            db.writeByte(FIELDTAG_NAME);
            db.write(key.getBytes());
            db.writeByte(0);
            //Write the age.
            db.writeShort(value.length() + 1);
            db.writeByte(FIELDTAG_AGE);
            db.write(value.getBytes());
            db.writeByte(0);
        }           
    } catch (Exception e) {
        retVal = false;
    }

    return retVal;
}

//Interprets and stores the data sent from the Desktop Manager.
public boolean setSyncData(DataBuffer db, int version) {
    int length;
    Hashtable table = new Hashtable();
    Vector keys = new Vector();
    Vector values = new Vector();
    boolean retVal = true;
    try {
        //Read until the end of the Databuffer.
        while (db.available() > 0) {
            //Read the length of the data.
            length = db.readShort();
            //Set the byte array to the length of the data.
            byte[] bytes = new byte[length];
            //Determine the type of data to be read (name or age).
            switch (db.readByte()) {
                case FIELDTAG_NAME:
                    db.readFully(bytes);
                    keys.addElement(new String(bytes).trim());
                    break;
                case FIELDTAG_AGE:
                    db.readFully(bytes);
                    values.addElement(new String(bytes).trim());
                    break;
            }
        }
    } catch (Exception e) {
        retVal = false;
    }
    for (int i = 0; i < keys.size(); i++) {
        table.put(keys.elementAt(i), values.elementAt(i));
    }

    try {
        //Store the new data in the persistent store object.
        fList = new UserList(table);
        store.setContents(fList);
        store.commit();
    } catch (Exception e) {
        retVal = false;
    }

    return retVal;
}}

Позиция ввода следующая:

public class SyncItemSample extends UiApplication {

private static PersistentObject store;

private static UserList userList;

static {
    store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}

public static void main(String[] args) {
    SyncItemSample app = new SyncItemSample();
    app.enterEventDispatcher();
}

public SyncItemSample() {
    UserListScreen userListScreen;
    //Check to see if the store exists on the BlackBerry.
    synchronized (store) {
        if (store.getContents() == null) {
            //Store does not exist, create it with default values
            userList = new UserList();
            store.setContents(userList);
            store.commit();
        } else {
            //Store exists, retrieve data from store.
            userList = (UserList)store.getContents();               
        }
    }
    //Create and push the UserListScreen.
    userListScreen = new UserListScreen(userList);
    pushScreen(userListScreen);
}}

А вот реализация экрана:

public final class UserListScreen extends MainScreen {

Vector fLabels = new Vector();
Vector fValues = new Vector();

VerticalFieldManager leftColumn = new VerticalFieldManager();
VerticalFieldManager rightColumn = new VerticalFieldManager();

UserList fList;

public UserListScreen(UserList list) {
    super();
    fList = list;
    //Create a horizontal field manager to hold the two vertical field
    //managers to display the names and ages in two columns.
    VerticalFieldManager inputManager = new VerticalFieldManager();
    HorizontalFieldManager backGround = new HorizontalFieldManager();

    //Array of fields to display the names and ages.

    LabelField title = new LabelField("User List",
    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);

    final TextField fld1 = new TextField(TextField.NO_NEWLINE);
    fld1.setLabel("input label");
    inputManager.add(fld1);
    final TextField fld2 = new TextField(TextField.NO_NEWLINE);
    fld2.setLabel("input value");
    inputManager.add(fld2);
    final ButtonField fld3 = new ButtonField();
    fld3.setChangeListener(new FieldChangeListener() {          
        public void fieldChanged(Field field, int context) {
            fList.getData().put(fld1.getText().trim(), fld2.getText().trim());
            refresh();
        }
    });
    fld3.setLabel("add");
    inputManager.add(fld3);     
    add(inputManager);
    //Add the column titles and a blank field to create a space.
    LabelField leftTitle = new LabelField("label ");
    leftColumn.add(leftTitle);
    LabelField rightTitle = new LabelField("value");
    rightColumn.add(rightTitle);

    refresh();

    //Add the two vertical columns to the horizontal field manager.
    backGround.add(leftColumn);
    backGround.add(rightColumn);
    //Add the horizontal field manager to the screen.
    add(backGround);
}

private void refresh() {
    leftColumn.deleteAll();
    rightColumn.deleteAll();
    fLabels.removeAllElements();
    fValues.removeAllElements();
    //Populate and add the name and age fields.
    Enumeration e = fList.getData().keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        String value = (String) fList.getData().get(key); 
        final LabelField tmp1 = new LabelField(key);
        final LabelField tmp2 = new LabelField(value);
        leftColumn.add(tmp1);
        rightColumn.add(tmp2);
        fLabels.addElement(tmp1);
        fValues.addElement(tmp2);
    }
}

public boolean onClose() {
    System.exit(0);
    return true;
}}

Итак, как вы видите, это должно быть очень легко ...

Так что все это я запускаю приложение, добавляю значения в объект Persistent, и они добавляются правильно, сохраняются во время перезагрузки устройства и так далее ... Когда я запускаю Desktop Manager и создаю резервную копию, создается впечатление, что UserList резервируется, так как размер резервной копии увеличивается вместе с добавлением новых данных в постоянное хранилище.

Но когда я запускаю «Wipe device» на моем BB 9300 (и все данные из постоянного хранилища очищаются, как и ожидалось), а затем запускаю Восстановление из только что созданного файла резервной копии - в Приложении ничего не обновляется, и постоянное хранилище кажется быть пустым.

В некоторых примерах я обнаружил добавление альтернативной точки входа "init", но я не могу настроить все так, как описано в моем EclipsePlugin

Не могли бы вы посоветовать мне, как хранить данные в файле резервной копии и как извлекать те же данные из резервной копии и загружать их обратно в приложение, или как регистрировать какие-либо события с помощью Desktop Manager?

1 Ответ

1 голос
/ 31 октября 2011

Если у кого-то возникла такая же проблема, вы можете попытаться отключить устройство, прежде чем стирать его.Странно, но это помогло:)

...