ghostwan
2/15/2017 - 6:48 PM

Use meta-data in manifest

Use meta-data in manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ghostwan.sampleapp">

	<application
			android:allowBackup="true"
			android:icon="@mipmap/ic_launcher"
			android:label="@string/app_name"
			android:supportsRtl="true"
			android:theme="@style/AppTheme">

		<meta-data android:name="qi:topic:1" android:resource="@raw/test"/>
		<meta-data android:name="qi:topic:2" android:resource="@raw/test2"/>
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>
private String META_TAG = "qi:topic"

private void fetchDialogContentInAllApps() {
        Date startDate = new Date();
        synchronized (dialogContent){
            dialogContent.clear();
            final PackageManager pm = getPackageManager();

            List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

            for (ApplicationInfo applicationInfo : packages) {
                if (applicationInfo.metaData != null) {
                    Bundle metaData = applicationInfo.metaData;
                    String packageName = applicationInfo.packageName;

                    ArrayList<Map> arrayList = null;
                    for (String metadataKey : metaData.keySet()) {
                        if (metadataKey.contains(META_TAG)) {
                            if (arrayList == null) {
                                arrayList = new ArrayList<>();
                                dialogContent.put(packageName, arrayList);
                            }
                            Integer resID = (Integer) metaData.get(metadataKey);
                            Map data = null;
                            try {
                                data = retrieveDialogCollaborative(packageName, resID);
                                arrayList.add(data);
                            } catch (PackageManager.NameNotFoundException e) {
                                Log.e(TAG, "error :", e);
                            }
                        }
                    }
                }
            }
        }
        Log.i(TAG, "Fetch app in "+ Util.displayTime(startDate));
        sendAppList();
    }

    private Map retrieveDialogCollaborative(String packageName, Integer resID) throws PackageManager.NameNotFoundException {
        PackageManager manager = getPackageManager();

        Configuration overrideConfiguration = getBaseContext().getResources().getConfiguration();

        Resources resources = manager.getResourcesForApplication(packageName);
        String[] locales = resources.getAssets().getLocales();
        Map<String, String> mapLocale = new HashMap<>();
        for (String slocale : locales) {
            Locale locale = new Locale(slocale);
            try{
                overrideConfiguration.setLocale(locale);
                resources.updateConfiguration(overrideConfiguration, null);
                InputStream inputStream = resources.openRawResource(resID);
                String content = new Scanner(inputStream, "UTF-8").useDelimiter("\\A").next();
                mapLocale.put(locale.toString(), content);
            }
            catch (Resources.NotFoundException ignored){
            }
        }
        return mapLocale;
    }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ghostwan.collaborativedialog">

	<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>

	<application
			android:allowBackup="true"
			android:icon="@mipmap/ic_launcher"
			android:label="@string/app_name"
			android:supportsRtl="true"
			android:theme="@style/AppTheme">
		<service
				android:name=".DCService"
				android:enabled="true"
				android:exported="true">
		</service>

		<activity android:name=".StartServiceActivity"
				android:theme="@android:style/Theme.Translucent.NoTitleBar">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>