• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.impl;
6 
7 import android.content.Context;
8 import android.content.pm.ApplicationInfo;
9 import android.content.pm.PackageManager;
10 
11 import androidx.annotation.VisibleForTesting;
12 
13 import org.chromium.net.impl.CronetLogger.CronetSource;
14 
15 /**
16  * Utility class for working with the AndroidManifest flags.
17  */
18 @VisibleForTesting
19 public final class CronetManifest {
CronetManifest()20     private CronetManifest() {}
21     // Individual apps can use this meta-data tag in their manifest to opt in for telemetry.
22     // Todo (colibie): Add this to the android documentation
23     static final String TELEMETRY_OPT_IN_META_DATA_STR = "org.chromium.net.EnableCronetTelemetry";
24 
25     @VisibleForTesting
isAppOptedInForTelemetry(Context ctx, CronetSource source)26     public static boolean isAppOptedInForTelemetry(Context ctx, CronetSource source) {
27         try {
28             // Check if app is opted in
29             ApplicationInfo info = ctx.getPackageManager().getApplicationInfo(
30                     ctx.getPackageName(), PackageManager.GET_META_DATA);
31 
32             // TODO(b/226553652): Enable logging if loaded from CRONET_PLAY_SERVICES, after testing
33             //  with select users
34 
35             // getBoolean returns false if the key is not found, which is what we want.
36             return info.metaData == null ? false
37                                          : info.metaData.getBoolean(TELEMETRY_OPT_IN_META_DATA_STR);
38         } catch (PackageManager.NameNotFoundException e) {
39             // This should never happen.
40             // The conservative thing is to assume the app HAS opted out.
41             return false;
42         }
43     }
44 }
45