OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
9 import android.content.pm.PackageInfo; | 9 import android.content.pm.PackageInfo; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
(...skipping 26 matching lines...) Expand all Loading... | |
37 return Build.BRAND; | 37 return Build.BRAND; |
38 } | 38 } |
39 | 39 |
40 @CalledByNative | 40 @CalledByNative |
41 public static String getAndroidBuildId() { | 41 public static String getAndroidBuildId() { |
42 return Build.ID; | 42 return Build.ID; |
43 } | 43 } |
44 | 44 |
45 /** | 45 /** |
46 * @return The build fingerprint for the current Android install. The value is truncated to a | 46 * @return The build fingerprint for the current Android install. The value is truncated to a |
47 * 128 characters as this is used for crash and UMA reporting, which should avoid huge | 47 * 128 characters as this is used for crash and UMA reporting, which should avoid huge |
48 * strings. | 48 * strings. |
49 */ | 49 */ |
50 @CalledByNative | 50 @CalledByNative |
51 public static String getAndroidBuildFingerprint() { | 51 public static String getAndroidBuildFingerprint() { |
52 return Build.FINGERPRINT.substring( | 52 return Build.FINGERPRINT.substring( |
53 0, Math.min(Build.FINGERPRINT.length(), MAX_FINGERPRINT_LENGTH)) ; | 53 0, Math.min(Build.FINGERPRINT.length(), MAX_FINGERPRINT_LENGTH)) ; |
54 } | 54 } |
55 | 55 |
56 @CalledByNative | 56 @CalledByNative |
57 public static String getDeviceManufacturer() { | 57 public static String getDeviceManufacturer() { |
58 return Build.MANUFACTURER; | 58 return Build.MANUFACTURER; |
59 } | 59 } |
60 | 60 |
61 @CalledByNative | 61 @CalledByNative |
62 public static String getDeviceModel() { | 62 public static String getDeviceModel() { |
63 return Build.MODEL; | 63 return Build.MODEL; |
64 } | 64 } |
65 | 65 |
66 @CalledByNative | 66 @CalledByNative |
67 public static String getGMSVersionCode(Context context) { | 67 public static String getGMSVersionCode() { |
68 String msg = "gms versionCode not available."; | 68 String msg = "gms versionCode not available."; |
69 try { | 69 try { |
70 PackageManager packageManager = context.getPackageManager(); | 70 PackageManager packageManager = |
71 ContextUtils.getApplicationContext().getPackageManager(); | |
71 PackageInfo packageInfo = packageManager.getPackageInfo("com.google. android.gms", 0); | 72 PackageInfo packageInfo = packageManager.getPackageInfo("com.google. android.gms", 0); |
72 msg = Integer.toString(packageInfo.versionCode); | 73 msg = Integer.toString(packageInfo.versionCode); |
73 } catch (NameNotFoundException e) { | 74 } catch (NameNotFoundException e) { |
74 Log.d(TAG, "GMS package is not found.", e); | 75 Log.d(TAG, "GMS package is not found.", e); |
75 } | 76 } |
76 return msg; | 77 return msg; |
77 } | 78 } |
78 | 79 |
79 @CalledByNative | 80 @CalledByNative |
80 public static String getPackageVersionCode(Context context) { | 81 public static String getPackageVersionCode() { |
81 String msg = "versionCode not available."; | 82 String msg = "versionCode not available."; |
82 try { | 83 try { |
83 PackageManager pm = context.getPackageManager(); | 84 PackageManager pm = ContextUtils.getApplicationContext().getPackageM anager(); |
estevenson
2017/03/27 21:00:54
optional nit: might be a little nicer to store con
Peter Wen
2017/03/27 22:31:06
Modified it a little bit by calling getPackageName
| |
84 PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); | 85 PackageInfo pi = |
86 pm.getPackageInfo(ContextUtils.getApplicationContext().getPa ckageName(), 0); | |
85 msg = ""; | 87 msg = ""; |
86 if (pi.versionCode > 0) { | 88 if (pi.versionCode > 0) { |
87 msg = Integer.toString(pi.versionCode); | 89 msg = Integer.toString(pi.versionCode); |
88 } | 90 } |
89 } catch (NameNotFoundException e) { | 91 } catch (NameNotFoundException e) { |
90 Log.d(TAG, msg); | 92 Log.d(TAG, msg); |
91 } | 93 } |
92 return msg; | 94 return msg; |
93 } | 95 } |
94 | 96 |
95 @CalledByNative | 97 @CalledByNative |
96 public static String getPackageVersionName(Context context) { | 98 public static String getPackageVersionName() { |
97 String msg = "versionName not available"; | 99 String msg = "versionName not available"; |
98 try { | 100 try { |
99 PackageManager pm = context.getPackageManager(); | 101 PackageManager pm = ContextUtils.getApplicationContext().getPackageM anager(); |
100 PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); | 102 PackageInfo pi = |
103 pm.getPackageInfo(ContextUtils.getApplicationContext().getPa ckageName(), 0); | |
101 msg = ""; | 104 msg = ""; |
102 if (pi.versionName != null) { | 105 if (pi.versionName != null) { |
103 msg = pi.versionName; | 106 msg = pi.versionName; |
104 } | 107 } |
105 } catch (NameNotFoundException e) { | 108 } catch (NameNotFoundException e) { |
106 Log.d(TAG, msg); | 109 Log.d(TAG, msg); |
107 } | 110 } |
108 return msg; | 111 return msg; |
109 } | 112 } |
110 | 113 |
111 @CalledByNative | 114 @CalledByNative |
112 public static String getPackageLabel(Context context) { | 115 public static String getPackageLabel() { |
113 // Third-party code does disk read on the getApplicationInfo call. http: //crbug.com/614343 | 116 // Third-party code does disk read on the getApplicationInfo call. http: //crbug.com/614343 |
114 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); | 117 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
115 try { | 118 try { |
116 PackageManager packageManager = context.getPackageManager(); | 119 PackageManager packageManager = |
117 ApplicationInfo appInfo = packageManager.getApplicationInfo(context. getPackageName(), | 120 ContextUtils.getApplicationContext().getPackageManager(); |
121 ApplicationInfo appInfo = packageManager.getApplicationInfo( | |
122 ContextUtils.getApplicationContext().getPackageName(), | |
118 PackageManager.GET_META_DATA); | 123 PackageManager.GET_META_DATA); |
119 CharSequence label = packageManager.getApplicationLabel(appInfo); | 124 CharSequence label = packageManager.getApplicationLabel(appInfo); |
120 return label != null ? label.toString() : ""; | 125 return label != null ? label.toString() : ""; |
121 } catch (NameNotFoundException e) { | 126 } catch (NameNotFoundException e) { |
122 return ""; | 127 return ""; |
123 } finally { | 128 } finally { |
124 StrictMode.setThreadPolicy(oldPolicy); | 129 StrictMode.setThreadPolicy(oldPolicy); |
125 } | 130 } |
126 } | 131 } |
127 | 132 |
128 @CalledByNative | 133 @CalledByNative |
129 public static String getPackageName(Context context) { | 134 public static String getPackageName() { |
130 String packageName = context != null ? context.getPackageName() : null; | 135 if (ContextUtils.getApplicationContext() == null) { |
131 return packageName != null ? packageName : ""; | 136 return ""; |
137 } | |
138 return ContextUtils.getApplicationContext().getPackageName(); | |
132 } | 139 } |
133 | 140 |
134 @CalledByNative | 141 @CalledByNative |
135 public static String getBuildType() { | 142 public static String getBuildType() { |
136 return Build.TYPE; | 143 return Build.TYPE; |
137 } | 144 } |
138 | 145 |
139 /** | 146 /** |
140 * Check if this is a debuggable build of Android. Use this to enable develo per-only features. | 147 * Check if this is a debuggable build of Android. Use this to enable develo per-only features. |
141 */ | 148 */ |
(...skipping 23 matching lines...) Expand all Loading... | |
165 | 172 |
166 /** | 173 /** |
167 * @return Whether the current app targets the SDK for at least O | 174 * @return Whether the current app targets the SDK for at least O |
168 */ | 175 */ |
169 public static boolean targetsAtLeastO(Context appContext) { | 176 public static boolean targetsAtLeastO(Context appContext) { |
170 return isAtLeastO() | 177 return isAtLeastO() |
171 && appContext.getApplicationInfo().targetSdkVersion | 178 && appContext.getApplicationInfo().targetSdkVersion |
172 == Build.VERSION_CODES.CUR_DEVELOPMENT; | 179 == Build.VERSION_CODES.CUR_DEVELOPMENT; |
173 } | 180 } |
174 } | 181 } |
OLD | NEW |