2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
15 package com.android.settings;
17 import com.android.internal.logging.MetricsLogger;
19 import android.annotation.Nullable;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.os.AsyncTask;
25 import android.text.TextUtils;
26 import android.util.Log;
27 import com.android.internal.logging.MetricsProto.MetricsEvent;
32 public class SharedPreferencesLogger implements SharedPreferences {
34 private final String mTag;
35 private final Context mContext;
37 public SharedPreferencesLogger(Context context, String tag) {
43 public Map<String, ?> getAll() {
48 public String getString(String key, @Nullable String defValue) {
53 public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
58 public int getInt(String key, int defValue) {
63 public long getLong(String key, long defValue) {
68 public float getFloat(String key, float defValue) {
73 public boolean getBoolean(String key, boolean defValue) {
78 public boolean contains(String key) {
83 public Editor edit() {
84 return new EditorLogger();
88 public void registerOnSharedPreferenceChangeListener(
89 OnSharedPreferenceChangeListener listener) {
93 public void unregisterOnSharedPreferenceChangeListener(
94 OnSharedPreferenceChangeListener listener) {
97 private void logValue(String key, String value) {
98 MetricsLogger.count(mContext, mTag + "/" + key + "|" + value, 1);
101 private void logPackageName(String key, String value) {
102 MetricsLogger.count(mContext, mTag + "/" + key, 1);
103 MetricsLogger.action(mContext, MetricsEvent.ACTION_GENERIC_PACKAGE,
104 mTag + "/" + key + "|" + value);
107 private void safeLogValue(String key, String value) {
108 new AsyncPackageCheck().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, key, value);
111 private class AsyncPackageCheck extends AsyncTask<String, Void, Void> {
113 protected Void doInBackground(String... params) {
114 String key = params[0];
115 String value = params[1];
116 PackageManager pm = mContext.getPackageManager();
118 // Check if this might be a component.
119 ComponentName name = ComponentName.unflattenFromString(value);
121 value = name.getPackageName();
123 } catch (Exception e) {
126 pm.getPackageInfo(value, PackageManager.MATCH_UNINSTALLED_PACKAGES);
127 logPackageName(key, value);
128 } catch (PackageManager.NameNotFoundException e) {
129 // Clearly not a package, lets log it.
130 logValue(key, value);
136 public class EditorLogger implements Editor {
138 public Editor putString(String key, @Nullable String value) {
139 safeLogValue(key, value);
144 public Editor putStringSet(String key, @Nullable Set<String> values) {
145 safeLogValue(key, TextUtils.join(",", values));
150 public Editor putInt(String key, int value) {
151 logValue(key, String.valueOf(value));
156 public Editor putLong(String key, long value) {
157 logValue(key, String.valueOf(value));
162 public Editor putFloat(String key, float value) {
163 logValue(key, String.valueOf(value));
168 public Editor putBoolean(String key, boolean value) {
169 logValue(key, String.valueOf(value));
174 public Editor remove(String key) {
179 public Editor clear() {
184 public boolean commit() {
189 public void apply() {