|
| 1 | +package com.blankj.subutil.util; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Service; |
| 5 | +import android.content.BroadcastReceiver; |
| 6 | +import android.content.ComponentName; |
| 7 | +import android.content.pm.ActivityInfo; |
| 8 | +import android.content.pm.ApplicationInfo; |
| 9 | +import android.content.pm.PackageManager; |
| 10 | +import android.content.pm.ServiceInfo; |
| 11 | +import android.support.annotation.NonNull; |
| 12 | + |
| 13 | +/** |
| 14 | + * <pre> |
| 15 | + * author: Blankj |
| 16 | + * blog : http://blankj.com |
| 17 | + * time : 2018/05/15 |
| 18 | + * desc : |
| 19 | + * </pre> |
| 20 | + */ |
| 21 | +public final class MetaDataUtils { |
| 22 | + |
| 23 | + private MetaDataUtils() { |
| 24 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Return the value of meta-data in application. |
| 29 | + * |
| 30 | + * @param key The key of meta-data. |
| 31 | + * @return the value of meta-data in application |
| 32 | + */ |
| 33 | + public static String getMetaDataInApp(@NonNull final String key) { |
| 34 | + String value = ""; |
| 35 | + PackageManager pm = Utils.getApp().getPackageManager(); |
| 36 | + String packageName = Utils.getApp().getPackageName(); |
| 37 | + try { |
| 38 | + ApplicationInfo ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); |
| 39 | + value = String.valueOf(ai.metaData.get(key)); |
| 40 | + } catch (PackageManager.NameNotFoundException e) { |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + return value; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Return the value of meta-data in activity. |
| 48 | + * |
| 49 | + * @param activity The activity. |
| 50 | + * @param key The key of meta-data. |
| 51 | + * @return the value of meta-data in activity |
| 52 | + */ |
| 53 | + public static String getMetaDataInActivity(@NonNull final Activity activity, |
| 54 | + @NonNull final String key) { |
| 55 | + return getMetaDataInActivity(activity.getClass(), key); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Return the value of meta-data in activity. |
| 60 | + * |
| 61 | + * @param clz The activity class. |
| 62 | + * @param key The key of meta-data. |
| 63 | + * @return the value of meta-data in activity |
| 64 | + */ |
| 65 | + public static String getMetaDataInActivity(@NonNull final Class<? extends Activity> clz, |
| 66 | + @NonNull final String key) { |
| 67 | + String value = ""; |
| 68 | + PackageManager pm = Utils.getApp().getPackageManager(); |
| 69 | + ComponentName componentName = new ComponentName(Utils.getApp(), clz); |
| 70 | + try { |
| 71 | + ActivityInfo ai = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA); |
| 72 | + value = String.valueOf(ai.metaData.get(key)); |
| 73 | + } catch (PackageManager.NameNotFoundException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + return value; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Return the value of meta-data in service. |
| 81 | + * |
| 82 | + * @param service The service. |
| 83 | + * @param key The key of meta-data. |
| 84 | + * @return the value of meta-data in service |
| 85 | + */ |
| 86 | + public static String getMetaDataInService(@NonNull final Service service, |
| 87 | + @NonNull final String key) { |
| 88 | + return getMetaDataInService(service.getClass(), key); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return the value of meta-data in service. |
| 93 | + * |
| 94 | + * @param clz The service class. |
| 95 | + * @param key The key of meta-data. |
| 96 | + * @return the value of meta-data in service |
| 97 | + */ |
| 98 | + public static String getMetaDataInService(@NonNull final Class<? extends Service> clz, |
| 99 | + @NonNull final String key) { |
| 100 | + String value = ""; |
| 101 | + PackageManager pm = Utils.getApp().getPackageManager(); |
| 102 | + ComponentName componentName = new ComponentName(Utils.getApp(), clz); |
| 103 | + try { |
| 104 | + ServiceInfo info = pm.getServiceInfo(componentName, PackageManager.GET_META_DATA); |
| 105 | + value = String.valueOf(info.metaData.get(key)); |
| 106 | + } catch (PackageManager.NameNotFoundException e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + return value; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Return the value of meta-data in receiver. |
| 114 | + * |
| 115 | + * @param receiver The receiver. |
| 116 | + * @param key The key of meta-data. |
| 117 | + * @return the value of meta-data in receiver |
| 118 | + */ |
| 119 | + public static String getMetaDataInReceiver(@NonNull final BroadcastReceiver receiver, |
| 120 | + @NonNull final String key) { |
| 121 | + return getMetaDataInReceiver(receiver, key); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Return the value of meta-data in receiver. |
| 126 | + * |
| 127 | + * @param clz The receiver class. |
| 128 | + * @param key The key of meta-data. |
| 129 | + * @return the value of meta-data in receiver |
| 130 | + */ |
| 131 | + public static String getMetaDataInReceiver(@NonNull final Class<? extends BroadcastReceiver> clz, |
| 132 | + @NonNull final String key) { |
| 133 | + String value = ""; |
| 134 | + PackageManager pm = Utils.getApp().getPackageManager(); |
| 135 | + ComponentName componentName = new ComponentName(Utils.getApp(), clz); |
| 136 | + try { |
| 137 | + ActivityInfo info = pm.getReceiverInfo(componentName, PackageManager.GET_META_DATA); |
| 138 | + value = String.valueOf(info.metaData.get(key)); |
| 139 | + } catch (PackageManager.NameNotFoundException e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + return value; |
| 143 | + } |
| 144 | +} |
0 commit comments