Merge pull request #1665 from kiwix/feature/macgills/1660-search-widget

#1660 Search Widget unresponsive in 3.1.2 - use explicit class name a…
This commit is contained in:
Seán Mac Gillicuddy 2019-12-19 14:38:13 +00:00 committed by GitHub
commit 3a5518fbe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 176 additions and 143 deletions

View File

@ -166,5 +166,19 @@
<activity android:name=".webserver.ZimHostActivity" />
<service android:name=".webserver.wifi_hotspot.HotspotService" />
<receiver android:name=".main.KiwixSearchWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="KiwixSearchWidget.TEXT_CLICKED" />
<action android:name="KiwixSearchWidget.ICON_CLICKED" />
<action android:name="KiwixSearchWidget.MIC_CLICKED" />
<action android:name="KiwixSearchWidget.STAR_CLICKED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/kiwix_widget_provider_info" />
</receiver>
</application>
</manifest>

View File

@ -0,0 +1,25 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.main
import org.kiwix.kiwixmobile.core.main.CoreSearchWidget
class KiwixSearchWidget : CoreSearchWidget() {
override val activityKClass = KiwixMainActivity::class
}

View File

@ -31,21 +31,6 @@
android:authorities="${applicationId}.zim.base"
android:exported="true" />
<receiver android:name=".main.KiwixSearchWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.TEXT_CLICKED" />
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.ICON_CLICKED" />
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.MIC_CLICKED" />
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.STAR_CLICKED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/kiwix_widget_provider_info" />
</receiver>
<activity
android:name=".error.ErrorActivity"
android:process=":error_activity" />

View File

@ -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);
}
}
}
}

View File

@ -0,0 +1,69 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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
)
}
}

View File

@ -1,85 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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);
}
}
}

View File

@ -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";

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/kiwix_search_widget"
android:minHeight="40dp"
android:minResizeHeight="40dp"
android:minResizeWidth="250dp"
android:minWidth="250dp"
android:minHeight="40dp"
android:minResizeWidth="250dp"
android:minResizeHeight="40dp"
android:previewImage="@drawable/search_widget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"></appwidget-provider>
android:updatePeriodMillis="86400000" />

View File

@ -58,5 +58,20 @@
</intent-filter>
</activity>
<activity android:name=".download.CustomDownloadActivity" />
<receiver android:name=".main.CustomSearchWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="KiwixSearchWidget.TEXT_CLICKED" />
<action android:name="KiwixSearchWidget.ICON_CLICKED" />
<action android:name="KiwixSearchWidget.MIC_CLICKED" />
<action android:name="KiwixSearchWidget.STAR_CLICKED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/kiwix_widget_provider_info" />
</receiver>
</application>
</manifest>

View File

@ -0,0 +1,25 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.custom.main
import org.kiwix.kiwixmobile.core.main.CoreSearchWidget
class CustomSearchWidget : CoreSearchWidget() {
override val activityKClass = CustomMainActivity::class
}