diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c12ccec27..5a085ddfa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -166,5 +166,19 @@ + + + + + + + + + + + + diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixSearchWidget.kt b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixSearchWidget.kt new file mode 100644 index 000000000..793987bd3 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixSearchWidget.kt @@ -0,0 +1,25 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.main + +import org.kiwix.kiwixmobile.core.main.CoreSearchWidget + +class KiwixSearchWidget : CoreSearchWidget() { + override val activityKClass = KiwixMainActivity::class +} diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index eefe85add..0ac9cfc68 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -31,21 +31,6 @@ android:authorities="${applicationId}.zim.base" android:exported="true" /> - - - - - - - - - - - - - diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java index bb40e9c63..a07557085 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java @@ -20,10 +20,7 @@ package org.kiwix.kiwixmobile.core.main; import android.Manifest; import android.annotation.SuppressLint; -import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; -import android.content.ComponentName; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -119,8 +116,6 @@ import static org.kiwix.kiwixmobile.core.utils.Constants.BOOKMARK_CHOSEN_REQUEST import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_CHOSE_X_TITLE; import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_CHOSE_X_URL; import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_EXTERNAL_LINK; -import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_IS_WIDGET_SEARCH; -import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_IS_WIDGET_STAR; import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_IS_WIDGET_VOICE; import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_SEARCH; import static org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_ZIM_FILE; @@ -264,19 +259,6 @@ public abstract class CoreMainActivity extends BaseActivity } }; - private static void updateWidgets(Context context) { - Intent intent = new Intent(context.getApplicationContext(), KiwixSearchWidget.class); - intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID, - // since it seems the onUpdate() is only fired on that: - AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); - int[] ids = widgetManager.getAppWidgetIds(new ComponentName(context, KiwixSearchWidget.class)); - - widgetManager.notifyAppWidgetViewDataChanged(ids, android.R.id.list); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - context.sendBroadcast(intent); - } - @Override public void onActionModeStarted(ActionMode mode) { if (actionMode == null) { @@ -1215,13 +1197,13 @@ public abstract class CoreMainActivity extends BaseActivity startActivityForResult(i, MainMenuKt.REQUEST_FILE_SEARCH); break; } - case KiwixSearchWidget.TEXT_CLICKED: + case CoreSearchWidget.TEXT_CLICKED: goToSearch(false); break; - case KiwixSearchWidget.STAR_CLICKED: + case CoreSearchWidget.STAR_CLICKED: goToBookmarks(); break; - case KiwixSearchWidget.MIC_CLICKED: + case CoreSearchWidget.MIC_CLICKED: goToSearch(true); break; case Intent.ACTION_VIEW: @@ -1236,7 +1218,6 @@ public abstract class CoreMainActivity extends BaseActivity break; } } - updateWidgets(this); updateNightMode(); } @@ -1261,18 +1242,27 @@ public abstract class CoreMainActivity extends BaseActivity @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); - boolean isWidgetSearch = intent.getBooleanExtra(EXTRA_IS_WIDGET_SEARCH, false); - boolean isWidgetVoiceSearch = intent.getBooleanExtra(EXTRA_IS_WIDGET_VOICE, false); - boolean isWidgetStar = intent.getBooleanExtra(EXTRA_IS_WIDGET_STAR, false); - - if (isWidgetStar && zimReaderContainer.getId() != null) { - goToBookmarks(); - } else if (isWidgetSearch && zimReaderContainer.getId() != null) { - goToSearch(false); - } else if (isWidgetVoiceSearch && zimReaderContainer.getId() != null) { - goToSearch(true); - } else if (isWidgetStar || isWidgetSearch || isWidgetVoiceSearch) { - manageZimFiles(0); + if (intent.getAction() != null) { + if (zimReaderContainer.getId() != null) { + switch (intent.getAction()) { + case CoreSearchWidget.STAR_CLICKED: + goToBookmarks(); + break; + case CoreSearchWidget.TEXT_CLICKED: + goToSearch(false); + break; + case CoreSearchWidget.MIC_CLICKED: + goToSearch(true); + break; + } + } else { + switch (intent.getAction()) { + case CoreSearchWidget.STAR_CLICKED: + case CoreSearchWidget.TEXT_CLICKED: + case CoreSearchWidget.MIC_CLICKED: + manageZimFiles(0); + } + } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreSearchWidget.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreSearchWidget.kt new file mode 100644 index 000000000..95367e8b2 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreSearchWidget.kt @@ -0,0 +1,69 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.main + +import android.app.PendingIntent +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.content.Context +import android.content.Intent +import android.widget.RemoteViews +import org.kiwix.kiwixmobile.core.R +import kotlin.reflect.KClass + +abstract class CoreSearchWidget : AppWidgetProvider() { + + abstract val activityKClass: KClass<*> + + override fun onUpdate( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetIds: IntArray + ) { + val appName = context.getString(R.string.app_name) + appWidgetIds.forEach { appWidgetId -> + val views = RemoteViews(context.packageName, R.layout.kiwix_search_widget) + views.setTextViewText(R.id.search_widget_text, "Search $appName") + idsToActions.forEach { (id, action) -> + views.setOnClickPendingIntent(id, pendingIntent(context, action)) + } + appWidgetManager.updateAppWidget(appWidgetId, views) + } + } + + private fun pendingIntent(context: Context, action: String) = PendingIntent.getActivity( + context, + (System.currentTimeMillis() % Int.MAX_VALUE).toInt(), + Intent(context, activityKClass.java).setAction(action), + 0 + ) + + companion object { + const val TEXT_CLICKED = "KiwixSearchWidget.TEXT_CLICKED" + const val MIC_CLICKED = "KiwixSearchWidget.MIC_CLICKED" + const val STAR_CLICKED = "KiwixSearchWidget.STAR_CLICKED" + private const val ICON_CLICKED = "KiwixSearchWidget.ICON_CLICKED" + + private val idsToActions = mapOf( + R.id.search_widget_text to TEXT_CLICKED, + R.id.search_widget_icon to ICON_CLICKED, + R.id.search_widget_star to STAR_CLICKED, + R.id.search_widget_mic to MIC_CLICKED + ) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixSearchWidget.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixSearchWidget.java deleted file mode 100644 index df40be935..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixSearchWidget.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.main; - -import android.app.PendingIntent; -import android.appwidget.AppWidgetManager; -import android.appwidget.AppWidgetProvider; -import android.content.Context; -import android.content.Intent; -import android.widget.RemoteViews; -import org.kiwix.kiwixmobile.core.Intents; -import org.kiwix.kiwixmobile.core.R; - -public class KiwixSearchWidget extends AppWidgetProvider { - - public static final String TEXT_CLICKED = - "KiwixSearchWidget.TEXT_CLICKED"; - public static final String ICON_CLICKED = - "KiwixSearchWidget.ICON_CLICKED"; - public static final String MIC_CLICKED = - "KiwixSearchWidget.MIC_CLICKED"; - public static final String STAR_CLICKED = - "KiwixSearchWidget.STAR_CLICKED"; - - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - - String appName = context.getApplicationContext().getResources().getString(R.string.app_name); - - for (int id : appWidgetIds) { - RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.kiwix_search_widget); - views.setTextViewText(R.id.search_widget_text, "Search " + appName); - /** Search Kiwix intent **/ - Intent mainIntent = Intents.internal(CoreMainActivity.class); - mainIntent.setAction(TEXT_CLICKED); - PendingIntent searchPendingIntent = - PendingIntent.getActivity(context, (int) (System.currentTimeMillis() % Integer.MAX_VALUE), - mainIntent, 0); - - /** Kiwix icon intent to main app **/ - Intent kiwixIconIntent = Intents.internal(CoreMainActivity.class); - kiwixIconIntent.setAction(ICON_CLICKED); - PendingIntent mainAppPendingIntent = - PendingIntent.getActivity(context, (int) (System.currentTimeMillis() % Integer.MAX_VALUE), - kiwixIconIntent, 0); - - /** Star icon intent to bookmarks **/ - Intent starIntent = Intents.internal(CoreMainActivity.class); - starIntent.setAction(STAR_CLICKED); - PendingIntent starPendingIntent = - PendingIntent.getActivity(context, (int) (System.currentTimeMillis() % Integer.MAX_VALUE), - starIntent, 0); - - /** Microphone icon intent for voice search **/ - Intent voiceIntent = Intents.internal(CoreMainActivity.class); - voiceIntent.setAction(MIC_CLICKED); - PendingIntent voicePendingIntent = - PendingIntent.getActivity(context, (int) (System.currentTimeMillis() % Integer.MAX_VALUE), - voiceIntent, 0); - - views.setOnClickPendingIntent(R.id.search_widget_text, searchPendingIntent); - views.setOnClickPendingIntent(R.id.search_widget_icon, mainAppPendingIntent); - views.setOnClickPendingIntent(R.id.search_widget_star, starPendingIntent); - views.setOnClickPendingIntent(R.id.search_widget_mic, voicePendingIntent); - - appWidgetManager.updateAppWidget(id, views); - } - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.java index f48db7b6d..5e6b90763 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.java @@ -65,11 +65,6 @@ public final class Constants { public static final String EXTRA_SEARCH = "search"; public static final String EXTRA_IS_WIDGET_VOICE = "isWidgetVoice"; - - public static final String EXTRA_IS_WIDGET_SEARCH = "isWidgetSearch"; - - public static final String EXTRA_IS_WIDGET_STAR = "isWidgetStar"; - public static final String HOTSPOT_SERVICE_CHANNEL_ID = "hotspotService"; public static final String EXTRA_WEBVIEWS_LIST = "webviewsList"; diff --git a/core/src/main/res/xml/kiwix_widget_provider_info.xml b/core/src/main/res/xml/kiwix_widget_provider_info.xml index 9684afec6..0e640cd9c 100644 --- a/core/src/main/res/xml/kiwix_widget_provider_info.xml +++ b/core/src/main/res/xml/kiwix_widget_provider_info.xml @@ -1,10 +1,10 @@ + android:updatePeriodMillis="86400000" /> diff --git a/custom/src/main/AndroidManifest.xml b/custom/src/main/AndroidManifest.xml index c88694477..ddaa8abb1 100644 --- a/custom/src/main/AndroidManifest.xml +++ b/custom/src/main/AndroidManifest.xml @@ -58,5 +58,20 @@ + + + + + + + + + + + + + diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomSearchWidget.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomSearchWidget.kt new file mode 100644 index 000000000..a809e7807 --- /dev/null +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomSearchWidget.kt @@ -0,0 +1,25 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.custom.main + +import org.kiwix.kiwixmobile.core.main.CoreSearchWidget + +class CustomSearchWidget : CoreSearchWidget() { + override val activityKClass = CustomMainActivity::class +}