From 93710971f344bf522e99be9c78a62b3a5efa0562 Mon Sep 17 00:00:00 2001 From: Braden Farmer Date: Mon, 19 Dec 2016 15:32:55 -0700 Subject: [PATCH] Persist dashboard widgets across service restarts --- .../farmerbb/taskbar/service/DashboardService.java | 106 +++++++++++++-------- 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java b/app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java index 77a65a5d..3e6a9c3e 100644 --- a/app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java +++ b/app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java @@ -74,8 +74,6 @@ public class DashboardService extends Service { private int maxSize; private int previouslySelectedCell = -1; - private boolean tutorialShown = false; - private View.OnClickListener ocl = new View.OnClickListener() { @Override public void onClick(View view) { @@ -144,28 +142,7 @@ public class DashboardService extends Service { int appWidgetId = intent.getExtras().getInt("appWidgetId", -1); int cellId = intent.getExtras().getInt("cellId", -1); - AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); - - DashboardCell cellLayout = cells.get(cellId); - AppWidgetHostView hostView = mAppWidgetHost.createView(DashboardService.this, appWidgetId, appWidgetInfo); - hostView.setAppWidget(appWidgetId, appWidgetInfo); - hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight()); - - Bundle bundle = new Bundle(); - bundle.putInt("cellId", cellId); - hostView.setTag(bundle); - - cellLayout.findViewById(R.id.empty).setVisibility(View.GONE); - cellLayout.setOnLongClickListener(olcl); - cellLayout.setOnGenericMotionListener(ogml); - cellLayout.setOnInterceptedLongPressListener(listener); - - LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard); - linearLayout.addView(hostView); - - Bundle bundle2 = (Bundle) cellLayout.getTag(); - bundle2.putInt("appWidgetId", appWidgetId); - cellLayout.setTag(bundle2); + addWidget(appWidgetId, cellId, true); } } }; @@ -178,21 +155,7 @@ public class DashboardService extends Service { if(intent.hasExtra("cellId")) { int cellId = intent.getExtras().getInt("cellId", -1); - DashboardCell cellLayout = cells.get(cellId); - Bundle bundle = (Bundle) cellLayout.getTag(); - - mAppWidgetHost.deleteAppWidgetId(bundle.getInt("appWidgetId")); - bundle.remove("appWidgetId"); - - LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard); - linearLayout.removeAllViews(); - - cellLayout.setTag(bundle); - cellLayout.setOnClickListener(cellOcl); - cellLayout.setOnHoverListener(cellOhl); - cellLayout.setOnLongClickListener(null); - cellLayout.setOnGenericMotionListener(null); - cellLayout.setOnInterceptedLongPressListener(null); + removeWidget(cellId); } } }; @@ -325,6 +288,14 @@ public class DashboardService extends Service { mAppWidgetManager = AppWidgetManager.getInstance(this); mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID); + mAppWidgetHost.startListening(); + + for(int i = 0; i < maxSize; i++) { + int appWidgetId = pref.getInt("dashboard_widget_" + Integer.toString(i), -1); + if(appWidgetId != -1) + addWidget(appWidgetId, i, false); + } + mAppWidgetHost.stopListening(); LocalBroadcastManager.getInstance(this).unregisterReceiver(toggleReceiver); @@ -379,9 +350,9 @@ public class DashboardService extends Service { } else startActivity(intent); - if(!tutorialShown) { - U.showToast(this, R.string.dashboard_tutorial); // TODO if num of saved widgets == 0 - tutorialShown = true; + if(!pref.getBoolean("dashboard_tutorial_shown", false)) { + U.showToastLong(this, R.string.dashboard_tutorial); + pref.edit().putBoolean("dashboard_tutorial_shown", true).apply(); } } @@ -496,4 +467,55 @@ public class DashboardService extends Service { intent.putExtra("cellId", cellId); LocalBroadcastManager.getInstance(DashboardService.this).sendBroadcast(intent); } + + private void addWidget(int appWidgetId, int cellId, boolean shouldSave) { + AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); + + DashboardCell cellLayout = cells.get(cellId); + AppWidgetHostView hostView = mAppWidgetHost.createView(DashboardService.this, appWidgetId, appWidgetInfo); + hostView.setAppWidget(appWidgetId, appWidgetInfo); + hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight()); + + Bundle bundle = new Bundle(); + bundle.putInt("cellId", cellId); + hostView.setTag(bundle); + + cellLayout.findViewById(R.id.empty).setVisibility(View.GONE); + cellLayout.setOnLongClickListener(olcl); + cellLayout.setOnGenericMotionListener(ogml); + cellLayout.setOnInterceptedLongPressListener(listener); + + LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard); + linearLayout.addView(hostView); + + Bundle bundle2 = (Bundle) cellLayout.getTag(); + bundle2.putInt("appWidgetId", appWidgetId); + cellLayout.setTag(bundle2); + + if(shouldSave) { + SharedPreferences pref = U.getSharedPreferences(this); + pref.edit().putInt("dashboard_widget_" + Integer.toString(cellId), appWidgetId).apply(); + } + } + + private void removeWidget(int cellId) { + DashboardCell cellLayout = cells.get(cellId); + Bundle bundle = (Bundle) cellLayout.getTag(); + + mAppWidgetHost.deleteAppWidgetId(bundle.getInt("appWidgetId")); + bundle.remove("appWidgetId"); + + LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard); + linearLayout.removeAllViews(); + + cellLayout.setTag(bundle); + cellLayout.setOnClickListener(cellOcl); + cellLayout.setOnHoverListener(cellOhl); + cellLayout.setOnLongClickListener(null); + cellLayout.setOnGenericMotionListener(null); + cellLayout.setOnInterceptedLongPressListener(null); + + SharedPreferences pref = U.getSharedPreferences(this); + pref.edit().remove("dashboard_widget_" + Integer.toString(cellId)).apply(); + } } -- 2.11.0