Example usage for android.content.pm PackageManager GET_META_DATA

List of usage examples for android.content.pm PackageManager GET_META_DATA

Introduction

In this page you can find the example usage for android.content.pm PackageManager GET_META_DATA.

Prototype

int GET_META_DATA

To view the source code for android.content.pm PackageManager GET_META_DATA.

Click Source Link

Document

ComponentInfo flag: return the ComponentInfo#metaData data android.os.Bundle s that are associated with a component.

Usage

From source file:Main.java

/**
 * //from  w ww .  j a va2  s . com
 * @param c
 * @param uid
 * @return
 * @throws JSONException
 */
public static JSONArray getPackagesForUid(Context c, int uid) throws JSONException {
    PackageManager pm = c.getPackageManager();
    JSONArray res = new JSONArray();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null) {
        for (int i = 0; i < pkgs.length; i++) {
            try {
                CharSequence appLabel = pm
                        .getApplicationLabel(pm.getApplicationInfo(pkgs[i], PackageManager.GET_META_DATA));
                JSONObject pkg = new JSONObject();
                pkg.put("package", pkgs[i]);
                pkg.put("app_label", appLabel.toString());
                res.put(pkg);
            } catch (NameNotFoundException e) {
            } catch (Exception e) {
            }
        }
    }
    return res;
}

From source file:Main.java

private static void checkBackupApiKey() {
    if (null == mContext) {
        return;/* w  w w.j a  va 2  s  .c om*/
    }

    boolean haveBackupApiKey = false;
    {
        final ApplicationInfo appInfo = mContext.getApplicationInfo();
        if (null != appInfo) {
            if (null != appInfo.metaData) {
                if (appInfo.metaData.containsKey(ANDROID_BACKUP_APIKEY)) {
                    final String backupApiKey = appInfo.metaData.getString(ANDROID_BACKUP_APIKEY);
                    Log.d(TAG, "backupApiKey from Context=" + backupApiKey);
                    haveBackupApiKey = true;
                } else {
                    Log.d(TAG, "metaData from Context not contain key=" + ANDROID_BACKUP_APIKEY);
                }
            }
        }
    }

    if (false == haveBackupApiKey) {
        final ApplicationInfo appInfoFromPM = getApplicationInfoFromPackageManager(
                PackageManager.GET_META_DATA);
        if (null != appInfoFromPM) {
            Log.d(TAG, "metaData from PakcageManager=" + appInfoFromPM.metaData);
            if (null != appInfoFromPM.metaData) {
                if (appInfoFromPM.metaData.containsKey(ANDROID_BACKUP_APIKEY)) {
                    final String backupApiKey = appInfoFromPM.metaData.getString(ANDROID_BACKUP_APIKEY);
                    Log.d(TAG, "backupApiKey from PackageManager=" + backupApiKey);
                    haveBackupApiKey = true;
                } else {
                    Log.d(TAG, "metaData from PackageManager not contain key=" + ANDROID_BACKUP_APIKEY);
                }
            }
        }

    }

    if (false == haveBackupApiKey) {
        Log.e(TAG, "AndroidManifest.xml doesn't contain\n" + "<application><meta-data android:name=\""
                + ANDROID_BACKUP_APIKEY + "\" android:value=\"xxxxxx\" /></application>");
        Log.e(TAG, "register url http://code.google.com/android/backup/signup.html");
    }

}

From source file:cn.whereyougo.framework.utils.PackageManagerUtil.java

/**
 * ?AndroidManifestmeta-data/*from www .j a  v a2 s  . co m*/
 * 
 * @return ??()
 */
public static String getStringMetaData(Context ctx, String key) {
    if (ctx == null || TextUtils.isEmpty(key)) {
        return null;
    }
    String resultData = null;
    try {
        PackageManager packageManager = ctx.getPackageManager();
        if (packageManager != null) {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(),
                    PackageManager.GET_META_DATA);
            if (applicationInfo != null) {
                if (applicationInfo.metaData != null) {
                    resultData = applicationInfo.metaData.getString(key);
                }
            }

        }
    } catch (PackageManager.NameNotFoundException e) {
        LogUtil.e(TAG, "", e);
    }

    return resultData;
}

From source file:org.deviceconnect.android.deviceplugin.chromecast.setting.ChromeCastSettingFragmentPage3.java

/**
 * Chromecast App (Google) ??./*from   www .  j a  va 2  s  .  c  om*/
 * 
 * @param   context         
 * @return      true: ???, false: ?????
 */
private boolean isApplicationInstalled(final Context context) {
    boolean installed = false;
    try {
        context.getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_META_DATA);
        installed = true;
    } catch (NameNotFoundException e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }
    return installed;
}

From source file:com.nokia.example.mapsv2oneapk.MainActivity.java

/**
 * Validate that AndroidManifest.xml defines keys this application requires
 *
 * @return//ww w. j  ava 2 s.  co  m
 */
private boolean validateMetaData() {
    try {
        ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(),
                PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        for (String key : REQUIRED_KEYS_ARRAY) {
            if (TextUtils.isEmpty(bundle.getString(key))) {
                Log.d(TAG, "missing meta-data from AndroidManifest.xml. Undefined key " + key);
                return false;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, e.getMessage());
    }
    return true;
}

From source file:com.github.michalbednarski.intentslab.browser.ApplicationFetcher.java

@Override
Object getEntries(Context context) {
    PackageManager pm = context.getPackageManager();
    int requestedPackageInfoFlags = PackageManager.GET_DISABLED_COMPONENTS
            | (requireMetaDataSubstring != null ? PackageManager.GET_META_DATA : 0);

    List<PackageInfo> allPackages = pm.getInstalledPackages(requestedPackageInfoFlags);

    ArrayList<Component> selectedApps = new ArrayList<Component>();

    for (PackageInfo pack : allPackages) {
        ApplicationInfo applicationInfo = pack.applicationInfo;

        // Filter out non-applications
        if (applicationInfo == null) {
            continue;
        }//  ww w .ja  v a 2  s  .c o m

        // System app filter
        if ((((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? APP_TYPE_SYSTEM : APP_TYPE_USER)
                & appType) == 0) {
            continue;
        }

        // Metadata filter
        if (!checkMetaDataFilter(applicationInfo)) {
            continue;
        }

        // Build and add app descriptor
        Component app = new Component();
        app.title = String.valueOf(applicationInfo.loadLabel(pm));
        app.subtitle = pack.packageName;
        app.componentInfo = applicationInfo;
        selectedApps.add(app);

        // Allow cancelling task
        if (Thread.interrupted()) {
            return null;
        }
    }
    return selectedApps.toArray(new Component[selectedApps.size()]);
}

From source file:com.clanofthecloud.cotcpushnotifications.RegistrationIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    try {//ww w.j  a va 2  s  .com
        // In the (unlikely) event that multiple refresh operations occur simultaneously,
        // ensure that they are processed sequentially.
        synchronized (TAG) {
            // [START register_for_gcm]
            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            // [START get_token]
            InstanceID instanceID = InstanceID.getInstance(this);
            ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(),
                    PackageManager.GET_META_DATA);
            Object senderId = ai.metaData.get("cotc.GcmSenderId");
            if (senderId == null) {
                Log.e(TAG,
                        "!!!!!!!!! cotc.GcmSenderId not configured in manifest, push notifications won't work !!!!!!!!!");
                senderId = "";
            }
            Log.v(TAG, "Using senderId: " + senderId.toString());
            String token = instanceID.getToken(senderId.toString(), GoogleCloudMessaging.INSTANCE_ID_SCOPE,
                    null);
            // [END get_token]
            Log.v(TAG, "GCM Registration Token: " + token);

            registrationToken = token;

            // Subscribe to topic channels
            subscribeTopics(token);
            // [END register_for_gcm]
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed to complete token refresh", e);
        // If an exception happens while fetching the new token or updating our registration data
        // on a third-party server, this ensures that we'll attempt the update at a later time.
        //            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
    }
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent(Controller.REGISTRATION_COMPLETE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

From source file:org.mariotaku.twidere.loader.ExtensionsListLoader.java

@Override
public List<ExtensionInfo> loadInBackground() {
    final List<ApplicationInfo> apps = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    final List<ExtensionInfo> extensions = new ArrayList<ExtensionInfo>();
    for (final ApplicationInfo info : apps) {
        final Bundle meta = info.metaData;
        if (meta != null && meta.getBoolean(METADATA_KEY_EXTENSION, false)) {
            extensions.add(new ExtensionInfo(info, mPackageManager));
        }//from   w ww.  j a  va2 s.c o  m
    }
    return extensions;
}

From source file:com.hybris.mobile.lib.commerce.sync.CatalogSyncService.java

@Override
public void onCreate() {
    Log.i(TAG, "Service for the CatalogSyncAdapter starting");

    // The sync adapter is created as a singleton
    synchronized (mSingletonLock) {
        if (mSyncAdapter == null) {

            try {
                ComponentName componentName = new ComponentName(this, this.getClass());
                Bundle bundle = getPackageManager().getServiceInfo(componentName,
                        PackageManager.GET_META_DATA).metaData;

                String urlBackend = bundle
                        .getString(getApplicationContext().getString(R.string.sync_url_backend_metadata_name));
                String urlPathCatalogMetadataName = bundle.getString(
                        getApplicationContext().getString(R.string.sync_url_path_catalog_metadata_name));
                String urlPathCatalogIdMetadataName = bundle.getString(
                        getApplicationContext().getString(R.string.sync_url_path_catalog_id_metadata_name));
                String urlPathCatalogVersionIdMetadataName = bundle.getString(getApplicationContext()
                        .getString(R.string.sync_url_path_catalog_version_id_metadata_name));
                String idMainCategory = bundle
                        .getString(getApplicationContext().getString(R.string.sync_id_category_main));

                if (StringUtils.isBlank(urlBackend) || StringUtils.isBlank(urlBackend)
                        || StringUtils.isBlank(urlBackend) || StringUtils.isBlank(idMainCategory)) {
                    throw new IllegalArgumentException("You must provide the metadata "
                            + getApplicationContext().getString(R.string.sync_url_backend_metadata_name) + ", "
                            + getApplicationContext().getString(R.string.sync_url_path_catalog_metadata_name)
                            + ", "
                            + getApplicationContext().getString(R.string.sync_url_path_catalog_id_metadata_name)
                            + ", "
                            + getApplicationContext()
                                    .getString(R.string.sync_url_path_catalog_version_id_metadata_name)
                            + ", " + getApplicationContext().getString(R.string.sync_id_category_main) + " for "
                            + this.getClass());
                }/*ww w. ja v a2  s .co m*/

                Configuration configuration = new Configuration();
                configuration.setBackendUrl(urlBackend);
                configuration.setCatalog(urlPathCatalogMetadataName);
                configuration.setCatalogId(urlPathCatalogIdMetadataName);
                configuration.setCatalogVersionId(urlPathCatalogVersionIdMetadataName);
                configuration.setCatalogIdMainCategory(idMainCategory);

                // Provider authority
                String provider_authority = getString(R.string.sync_default_authority);

                try {
                    componentName = new ComponentName(this, CatalogProvider.class);

                    // Authority name from the manifest file
                    String tmpAuthority = getPackageManager().getProviderInfo(componentName,
                            PackageManager.GET_META_DATA).authority;

                    if (StringUtils.isNotBlank(tmpAuthority)) {
                        provider_authority = tmpAuthority;
                    }

                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(TAG, "Package name not found. Details:" + e.getLocalizedMessage());
                }

                configuration.setCatalogAuthority(provider_authority);

                // SSL Helper for the ssl factory and hostname verifier
                String sslHelperClassName = bundle
                        .getString(getApplicationContext().getString(R.string.sync_ssl_helper));
                SSLSocketFactory sslSocketFactory = null;
                HostnameVerifier hostnameVerifier = null;

                if (StringUtils.isNotBlank(sslHelperClassName)) {
                    SSLHelper sslHelper = null;
                    String errorMessage = null;
                    try {
                        Class sslFactory = Class.forName(sslHelperClassName);
                        Constructor<SSLHelper> constructor = sslFactory.getConstructor();
                        sslHelper = constructor.newInstance();

                        sslSocketFactory = sslHelper.getSSLSocketFactory();
                        hostnameVerifier = sslHelper.getHostnameVerifier();
                    } catch (ClassNotFoundException e) {
                        errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, "ClassNotFoundException with SSLHelper. Details: " + errorMessage);
                    } catch (InvocationTargetException e) {
                        errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, "InvocationTargetException with SSLHelper. Details: " + errorMessage);
                    } catch (NoSuchMethodException e) {
                        errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, "NoSuchMethodException with SSLHelper. Details: " + errorMessage);
                    } catch (InstantiationException e) {
                        errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, "InstantiationException with SSLHelper. Details: " + errorMessage);
                    } catch (IllegalAccessException e) {
                        errorMessage = e.getLocalizedMessage();
                        Log.e(TAG, "IllegalAccessException with SSLHelper. Details: " + errorMessage);
                    }

                    if (sslHelper == null) {
                        throw new IllegalArgumentException(
                                "Error instantiating SSLHelper. Details: " + errorMessage);
                    }
                }

                // Create the sync adapter
                mSyncAdapter = buildSyncAdapter(configuration, sslSocketFactory, hostnameVerifier);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Package name not found. Details:" + e.getLocalizedMessage());
                throw new IllegalArgumentException(
                        "Error getting the information from the metadata of " + this.getClass());
            }

        }
    }
}

From source file:com.manotaurgames.castro.CastFragment.java

private String getCastAppId() {
    if (mCastAppIdRes != null) {
        return getString(mCastAppIdRes);
    }/*  w  ww.  ja va2 s .  co m*/
    Activity a = getActivity();
    try {
        ApplicationInfo ai = a.getPackageManager().getApplicationInfo(a.getPackageName(),
                PackageManager.GET_META_DATA);
        return (String) ai.metaData.get("GoogleCastId");
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }
}