-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize plugins and custom maps in MapTrek instead of MainActivity (…
…#193) This is necessary because MainActivity can be recreated with a saved bundle in two different cases: - on orientation changes, when only the activity is recreated - after the process has been killed In the first case, mMapIndex = application.getExtraMapIndex() worked well, though in the second case, MapTrek.mExtraMapIndex got cleared and maps from content providers hadn't had any chance to get initialized up to now. Co-authored-by: Andrey Novikov <[email protected]>
- Loading branch information
1 parent
460289b
commit 310d5b7
Showing
4 changed files
with
168 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/src/main/java/mobi/maptrek/plugin/PluginRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2022 Andrey Novikov | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package mobi.maptrek.plugin; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.ResolveInfo; | ||
import android.content.res.Resources; | ||
import android.graphics.drawable.Drawable; | ||
import android.util.Pair; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import mobi.maptrek.util.ContextUtils; | ||
|
||
public final class PluginRepository { | ||
|
||
private final Context mContext; | ||
|
||
// Plugins | ||
private final AbstractMap<String, Intent> mPluginPreferences = new HashMap<>(); | ||
private final AbstractMap<String, Pair<Drawable, Intent>> mPluginTools = new HashMap<>(); | ||
|
||
public PluginRepository(@NonNull Context context) { | ||
mContext = context; | ||
} | ||
|
||
public AbstractMap<String, Pair<Drawable, Intent>> getPluginTools() { | ||
return mPluginTools; | ||
} | ||
|
||
public AbstractMap<String, Intent> getPluginPreferences() { | ||
return mPluginPreferences; | ||
} | ||
|
||
public void addPluginEntry(Pair<String, Pair<Drawable, Intent>> entry) { | ||
mPluginTools.put(entry.first, entry.second); | ||
} | ||
|
||
public void initializePlugins() { | ||
PackageManager packageManager = mContext.getPackageManager(); | ||
List<ResolveInfo> plugins; | ||
|
||
// enumerate initializable plugins | ||
ContextUtils.sendExplicitBroadcast(mContext, "mobi.maptrek.plugins.action.INITIALIZE"); | ||
|
||
// enumerate plugins with preferences | ||
plugins = packageManager.queryIntentActivities(new Intent("mobi.maptrek.plugins.preferences"), 0); | ||
for (ResolveInfo plugin : plugins) { | ||
Intent intent = new Intent(); | ||
intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name); | ||
mPluginPreferences.put(plugin.activityInfo.loadLabel(packageManager).toString(), intent); | ||
} | ||
|
||
// enumerate plugins with views | ||
plugins = packageManager.queryIntentActivities(new Intent("mobi.maptrek.plugins.tool"), 0); | ||
for (ResolveInfo plugin : plugins) { | ||
// get menu icon | ||
Drawable icon = null; | ||
try { | ||
Resources resources = packageManager.getResourcesForApplication(plugin.activityInfo.applicationInfo); | ||
int id = resources.getIdentifier("ic_menu_tool", "drawable", plugin.activityInfo.packageName); | ||
if (id != 0) | ||
icon = resources.getDrawable(id, mContext.getTheme()); | ||
} catch (Resources.NotFoundException | PackageManager.NameNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
Intent intent = new Intent(); | ||
intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name); | ||
Pair<Drawable, Intent> pair = new Pair<>(icon, intent); | ||
mPluginTools.put(plugin.activityInfo.loadLabel(packageManager).toString(), pair); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2022 Andrey Novikov | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package mobi.maptrek.util; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.ResolveInfo; | ||
|
||
import java.util.List; | ||
|
||
public class ContextUtils { | ||
public static void sendExplicitBroadcast(Context context, String action) { | ||
PackageManager packageManager = context.getPackageManager(); | ||
Intent intent = new Intent(action); | ||
List<ResolveInfo> plugins = packageManager.queryBroadcastReceivers(intent, 0); | ||
for (ResolveInfo plugin : plugins) { | ||
intent = new Intent(action); | ||
intent.setClassName(plugin.activityInfo.packageName, plugin.activityInfo.name); | ||
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); | ||
context.sendBroadcast(intent); | ||
} | ||
} | ||
} |