Search code examples
androidwidgetlauncher

How can I save and restore AndroidWidgetHostViews


I'm trying to build a custom launcher app. I'm stuck at restoring widgets when activity is restarted.

After picking a widget the widgetID is stored to shared prefs and in onCreate I'm trying to restore the previous set widgets. I was following this tutorial.

  AppWidgetManager mAppWidgetManager;
AppWidgetHost mAppWidgetHost;
ViewGroup hostView;
int numWidgets = 0;


/**
 * Called on the creation of the activity.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    hostView = (ViewGroup) findViewById(R.id.LAYOUT_DASHBOARD);

    hostView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
           if(numWidgets < 4) {
               selectWidget();
           }else {
               removeAllWidgets();
               numWidgets = 0;
           }
            return false;
        }
    });

    mAppWidgetManager = AppWidgetManager.getInstance(this);
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);



    restoreWidgets();
}

/**
 * Launches the menu to select the widget. The selected widget will be on
 * the result of the activity.
 */
void selectWidget() {
    int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    addEmptyData(pickIntent);
    startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
}

/**
 * This avoids a bug in the com.android.settings.AppWidgetPickActivity,
 * which is used to select widgets. This just adds empty extras to the
 * intent, avoiding the bug.
 *
 * See more: http://code.google.com/p/android/issues/detail?id=4272
 */
void addEmptyData(Intent pickIntent) {
    ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
}

/**
 * If the user has selected an widget, the result will be in the 'data' when
 * this function is called.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
            configureWidget(data);
        } else if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
            createWidget(data);
        }
    } else if (resultCode == RESULT_CANCELED && data != null) {
        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        if (appWidgetId != -1) {
            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

/**
 * Checks if the widget needs any configuration. If it needs, launches the
 * configuration activity.
 */
private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
    } else {
        createWidget(data);
    }
}

/**
 * Creates the widget and adds to our view layout.
 */
public void createWidget(Intent data) {
    if(numWidgets<4) {
        Bundle extras = data.getExtras();
        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);

        AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
        AppWidgetHostView widHostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
        widHostView.setAppWidget(appWidgetId, appWidgetInfo);

        SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("ID"+numWidgets, appWidgetId);

        editor.commit();
        attachWidget(widHostView);

    }
}

/**
 * Attaches a new widget at the right position on hostView
 * @param widHostView   widget to attach
 */
public void attachWidget(AppWidgetHostView widHostView){
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(hostView.getWidth() / 2, hostView.getHeight() / 2);
    if (numWidgets < 2) {
        lp.leftMargin = numWidgets * (hostView.getWidth() / 2);
        lp.topMargin = 0;
    } else {
        lp.leftMargin = (numWidgets - 2) * (hostView.getWidth() / 2);
        lp.topMargin = hostView.getHeight() / 2;
    }

    this.hostView.addView(widHostView, lp);
    numWidgets++;
}

/**
 * Restores all widgets from shared preferences
 */
public void restoreWidgets()
{
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    restoreWidget(sharedPref.getInt("ID0", -1));
    restoreWidget(sharedPref.getInt("ID1", -1));
    restoreWidget(sharedPref.getInt("ID2", -1));
    restoreWidget(sharedPref.getInt("ID3", -1));
}

/**
 * Restores a single widget
 */
public void restoreWidget(int _widgetId){
    if(_widgetId < 0){
        return;
    }
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(_widgetId);
    AppWidgetHostView hostView = mAppWidgetHost.createView(this, _widgetId, appWidgetInfo);
    hostView.setAppWidget(_widgetId, appWidgetInfo);
    attachWidget(hostView);
}

/**
 * Registers the AppWidgetHost to listen for updates to any widgets this app
 * has.
 */
@Override
protected void onStart() {
    super.onStart();
    mAppWidgetHost.startListening();
}

/**
 * Stop listen for updates for our widgets (saving battery).
 */
@Override
protected void onStop() {
    super.onStop();
    mAppWidgetHost.stopListening();
}

/**
 * Removes the widget displayed by this AppWidgetHostView.
 */
public void removeWidget(AppWidgetHostView hostView) {
    mAppWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId());
    this.hostView.removeView(hostView);
}

/**
 * Removes all widgets displayed on screen.
 */
public void removeAllWidgets(){
   boolean stop = false;
    do{
        int childCount = hostView.getChildCount();
        if (childCount >= 1) {
            View view = hostView.getChildAt(childCount - 1);
            if (view instanceof AppWidgetHostView) {
                removeWidget((AppWidgetHostView) view);
            }
        }else{
            stop = true;
        }
    }while (!stop);
}

}

Solution

  • I did some debugging for this and realized, that you need to store the appWidgetIds for each widget added. When I stored appWidgetIds and tried to get AppWidgetProviderInfo from them, the width and height was always 0, BUT mAppWidgetHost.createView would return a valid view. So to fix this store the last width and height, the id, and any other data you wish to a WidgetHolder. Then you can store those values into SharedPreferences if you wish.

    Here is a working WidgetHolder class I wrote ...

    import android.util.Base64;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class WidgetHolder implements Serializable {
    
        // any other data you want
        public int id;
    
        public int width, height;
    
        public WidgetHolder() {}
    
        public WidgetHolder(int id, int width, int height) {
            this.id = id;
            this.width = width;
            this.height = height;
        }
    
        public static WidgetHolder deserialize( String data ) {
            try {
                byte [] bites = Base64.decode( data.getBytes(), 0);
                ByteArrayInputStream bis = new ByteArrayInputStream( bites );
                ObjectInputStream ois = new ObjectInputStream( bis );
                WidgetHolder holder = (WidgetHolder) ois.readObject();
                ois.close();
                return holder;
            } catch (IOException e ) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return new WidgetHolder();
        }
    
        public String serialize() {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream( baos );
                oos.writeObject(this);
                oos.flush();
                return new String(Base64.encode(baos.toByteArray(), 0));
            } catch( IOException e ) {
                return "";
            }
        }
    }
    

    Then whenever you add a new AppWidget to your AppWidgetHost you store the WidgetHolder data to SharedPreferences or a cached file.

    private void handleAddNewAppWidget( int appWidgetId, AppWidgetProviderInfo info ) {
        // use application context, for issues with support v7 libs causing AppCompatImage exceptions
        AppWidgetHostView hostView = mAppWidgetHost
                .createView(context.getApplicationContext(), appWidgetId, info);   
        hostView.setAppWidget(appWidgetId, info);
    
        // calculate width and height for hostView from info.minHeight and info.minWidth
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( width, height );
        hostView.setLayoutParams(rlp);
    
        // add hostView to widgetContainer (RelativeLayout)
        widgetContainer.addView(hostView);
    
        // create widgetHolder
        WidgetHolder holder = new WidgetHolder( appWidgetId, width, height );
    
        String serialized = holder.serialize();
    
        // storage is a wrapper for SharedPreferences, the key is the appWidgetId, and the value is the serialized content
        storage.store(appWidgetId + "", serialized);
    }
    
    private void handleRestoreWidgets(ArrayList<String> widgets) {
        // Retrieve all saved widgets from Storage
        for( int i = 0; i < widgets.size(); i++ ) {
            String widgetString = widgets.get(i);
            WidgetHolder holder = WidgetHolder.deserialize(widgetString);
            AppWidgetProviderInfo info = AppWidgetManager
                    .getInstance(getContext().getApplicationContext() )
                    .getAppWidgetInfo(holder.id);
    
            AppWidgetHostView hostView = mAppWidgetHost
                    .createView(context.getApplicationContext(), holder.id, info);
            hostView.setAppWidget(holder.id, info);
    
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( holder.width,
                    holder.height );
            hostView.setLayoutParams(rlp);
    
            // restore app widget from holder
            widgetContainer.addView(hostView);
        }
    }
    

    The Storage class was not included for brevity as this post is already long. However, to get all key-value pairs from SharedPreferences is easy.

    A small example ...

    Map<String, String> allKeyValues = getContext().getSharedPreferences("WidgetHolders", Context.MODE_PRIVATE).getAll();
    
    // then to get all stored String do 
    ArrayList<String> serializedWidgetHolders = new ArrayList<String>( allKeyValues.values() );