From 746afd18c8272bc7d3690f455313dc14152d95d7 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 19 Feb 2015 16:55:27 +0100 Subject: [PATCH] cpp-115: Unnecessary widgets Initial commit let's disable old widgets which are unused. --- android-app/src/main/AndroidManifest.xml | 13 ++++++ .../org/solovyev/android/calculator/App.java | 46 ++++++++++++++----- .../ExternalCalculatorIntentHandler.java | 38 --------------- ...java => BaseCalculatorWidgetProvider.java} | 6 +-- .../calculator/widget/CalculatorWidget.java | 4 ++ .../widget/CalculatorWidgetProvider.java | 2 +- .../widget/CalculatorWidgetProvider3x4.java | 2 +- .../widget/CalculatorWidgetProvider4x4.java | 2 +- .../widget/CalculatorWidgetProvider4x5.java | 2 +- .../main/res/xml/calculator_widget_info.xml | 34 ++++++++++++++ 10 files changed, 92 insertions(+), 57 deletions(-) delete mode 100644 android-app/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorIntentHandler.java rename android-app/src/main/java/org/solovyev/android/calculator/widget/{AbstractCalculatorWidgetProvider.java => BaseCalculatorWidgetProvider.java} (97%) create mode 100644 android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java create mode 100644 android-app/src/main/res/xml/calculator_widget_info.xml diff --git a/android-app/src/main/AndroidManifest.xml b/android-app/src/main/AndroidManifest.xml index 885df7d0..3431bffb 100644 --- a/android-app/src/main/AndroidManifest.xml +++ b/android-app/src/main/AndroidManifest.xml @@ -112,6 +112,19 @@ + + + + + + + + + + + + + diff --git a/android-app/src/main/java/org/solovyev/android/calculator/App.java b/android-app/src/main/java/org/solovyev/android/calculator/App.java index f6175b7e..a708d000 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/App.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/App.java @@ -23,20 +23,25 @@ package org.solovyev.android.calculator; import android.app.Application; -import android.content.Context; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.ComponentName; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; import android.preference.PreferenceManager; -import android.text.TextUtils; -import android.util.Log; +import org.solovyev.android.Android; import org.solovyev.android.UiThreadExecutor; import org.solovyev.android.Views; import org.solovyev.android.calculator.ga.Ga; import org.solovyev.android.calculator.view.ScreenMetrics; +import org.solovyev.android.calculator.widget.BaseCalculatorWidgetProvider; +import org.solovyev.android.calculator.widget.CalculatorWidgetProvider; +import org.solovyev.android.calculator.widget.CalculatorWidgetProvider3x4; +import org.solovyev.android.calculator.widget.CalculatorWidgetProvider4x4; +import org.solovyev.android.calculator.widget.CalculatorWidgetProvider4x5; import org.solovyev.android.checkout.*; -import org.solovyev.android.view.VibratorContainer; import org.solovyev.common.listeners.JEvent; import org.solovyev.common.listeners.JEventListener; import org.solovyev.common.listeners.JEventListeners; @@ -46,7 +51,9 @@ import org.solovyev.common.threads.DelayedExecutor; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.concurrent.Executor; /** @@ -62,14 +69,8 @@ import java.util.concurrent.Executor; * Before first usage this class must be initialized by calling {@link App#init(android.app.Application)} method (for example, from {@link android.app.Application#onCreate()}) */ public final class App { - - /* - ********************************************************************** - * - * FIELDS - * - ********************************************************************** - */ + @Nonnull + private static final List> OLD_WIDGETS = Arrays.asList(CalculatorWidgetProvider.class, CalculatorWidgetProvider3x4.class, CalculatorWidgetProvider4x4.class, CalculatorWidgetProvider4x5.class); @Nonnull private static volatile Application application; @@ -166,12 +167,33 @@ public final class App { App.broadcaster = new CalculatorBroadcaster(application); App.vibrator = new Vibrator(application, preferences); App.screenMetrics = new ScreenMetrics(application); + + final List> oldNotUsedWidgetClasses = findNotUsedWidgets(application); + for (Class oldNotUsedWidgetClass : oldNotUsedWidgetClasses) { + Android.enableComponent(application, oldNotUsedWidgetClass, false); + } + App.initialized = true; } else { throw new IllegalStateException("Already initialized!"); } } + @Nonnull + private static List> findNotUsedWidgets(@Nonnull Application application) { + final List> result = new ArrayList<>(); + + final AppWidgetManager widgetManager = AppWidgetManager.getInstance(application); + for (Class widgetClass : OLD_WIDGETS) { + final int ids[] = widgetManager.getAppWidgetIds(new ComponentName(application, widgetClass)); + if (ids == null || ids.length == 0) { + result.add(widgetClass); + } + } + + return result; + } + private static void checkInit() { if (!initialized) { throw new IllegalStateException("App should be initialized!"); diff --git a/android-app/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorIntentHandler.java b/android-app/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorIntentHandler.java deleted file mode 100644 index 4540558f..00000000 --- a/android-app/src/main/java/org/solovyev/android/calculator/external/ExternalCalculatorIntentHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * 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. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.external; - -import android.content.Context; -import android.content.Intent; - -import javax.annotation.Nonnull; - -/** - * User: serso - * Date: 11/20/12 - * Time: 10:33 PM - */ -public interface ExternalCalculatorIntentHandler { - - void onIntent(@Nonnull Context context, @Nonnull Intent intent); -} diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/BaseCalculatorWidgetProvider.java similarity index 97% rename from android-app/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java rename to android-app/src/main/java/org/solovyev/android/calculator/widget/BaseCalculatorWidgetProvider.java index 4e53e0fb..35000e3a 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/widget/AbstractCalculatorWidgetProvider.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/BaseCalculatorWidgetProvider.java @@ -49,7 +49,7 @@ import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClicke * Date: 19.10.12 * Time: 16:18 */ -abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { +public abstract class BaseCalculatorWidgetProvider extends AppWidgetProvider { private static final String TAG = "Calculator++ Widget"; private static final int WIDGET_CATEGORY_KEYGUARD = 2; @@ -75,7 +75,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { ********************************************************************** */ - protected AbstractCalculatorWidgetProvider() { + protected BaseCalculatorWidgetProvider() { } /* @@ -116,7 +116,7 @@ abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider { } @Nonnull - protected Class getComponentClass() { + protected Class getComponentClass() { return this.getClass(); } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java new file mode 100644 index 00000000..8e7695ad --- /dev/null +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -0,0 +1,4 @@ +package org.solovyev.android.calculator.widget; + +public class CalculatorWidget extends BaseCalculatorWidgetProvider { +} diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider.java index d53a7cd4..f8785be3 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider.java @@ -27,5 +27,5 @@ package org.solovyev.android.calculator.widget; * Date: 11/18/12 * Time: 1:00 PM */ -public class CalculatorWidgetProvider extends AbstractCalculatorWidgetProvider { +public class CalculatorWidgetProvider extends BaseCalculatorWidgetProvider { } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider3x4.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider3x4.java index bcf2f83b..5e88bd60 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider3x4.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider3x4.java @@ -27,5 +27,5 @@ package org.solovyev.android.calculator.widget; * Date: 11/18/12 * Time: 1:01 PM */ -public class CalculatorWidgetProvider3x4 extends AbstractCalculatorWidgetProvider { +public class CalculatorWidgetProvider3x4 extends BaseCalculatorWidgetProvider { } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x4.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x4.java index e8ddbfe9..459db813 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x4.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x4.java @@ -27,5 +27,5 @@ package org.solovyev.android.calculator.widget; * Date: 11/18/12 * Time: 1:01 PM */ -public class CalculatorWidgetProvider4x4 extends AbstractCalculatorWidgetProvider { +public class CalculatorWidgetProvider4x4 extends BaseCalculatorWidgetProvider { } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x5.java b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x5.java index 5130d615..4fac4dcd 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x5.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidgetProvider4x5.java @@ -27,5 +27,5 @@ package org.solovyev.android.calculator.widget; * Date: 11/18/12 * Time: 7:43 PM */ -public class CalculatorWidgetProvider4x5 extends AbstractCalculatorWidgetProvider { +public class CalculatorWidgetProvider4x5 extends BaseCalculatorWidgetProvider { } diff --git a/android-app/src/main/res/xml/calculator_widget_info.xml b/android-app/src/main/res/xml/calculator_widget_info.xml new file mode 100644 index 00000000..8ca2536e --- /dev/null +++ b/android-app/src/main/res/xml/calculator_widget_info.xml @@ -0,0 +1,34 @@ + + + + + + + \ No newline at end of file