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.core.instrumentation;
17 import android.annotation.Nullable;
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.pm.PackageManager;
22 import android.os.AsyncTask;
23 import android.text.TextUtils;
24 import android.util.Log;
25 import android.util.Pair;
27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
28 import com.android.settings.overlay.FeatureFactory;
32 import java.util.concurrent.ConcurrentSkipListSet;
34 public class SharedPreferencesLogger implements SharedPreferences {
36 private static final String LOG_TAG = "SharedPreferencesLogger";
38 private final String mTag;
39 private final Context mContext;
40 private final MetricsFeatureProvider mMetricsFeature;
41 private final Set<String> mPreferenceKeySet;
43 public SharedPreferencesLogger(Context context, String tag) {
46 mMetricsFeature = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
47 mPreferenceKeySet = new ConcurrentSkipListSet<>();
51 public Map<String, ?> getAll() {
56 public String getString(String key, @Nullable String defValue) {
61 public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
66 public int getInt(String key, int defValue) {
71 public long getLong(String key, long defValue) {
76 public float getFloat(String key, float defValue) {
81 public boolean getBoolean(String key, boolean defValue) {
86 public boolean contains(String key) {
91 public Editor edit() {
92 return new EditorLogger();
96 public void registerOnSharedPreferenceChangeListener(
97 OnSharedPreferenceChangeListener listener) {
101 public void unregisterOnSharedPreferenceChangeListener(
102 OnSharedPreferenceChangeListener listener) {
105 private void logValue(String key, Object value) {
106 logValue(key, value, false /* forceLog */);
109 private void logValue(String key, Object value, boolean forceLog) {
110 final String prefKey = mTag + "/" + key;
111 if (!forceLog && !mPreferenceKeySet.contains(prefKey)) {
112 // Pref key doesn't exist in set, this is initial display so we skip metrics but
113 // keeps track of this key.
114 mPreferenceKeySet.add(prefKey);
117 // TODO: Remove count logging to save some resource.
118 mMetricsFeature.count(mContext, prefKey + "|" + value, 1);
120 final Pair<Integer, Object> valueData;
121 if (value instanceof Long) {
122 valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
124 } else if (value instanceof Integer) {
125 valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
126 ((Integer) value).longValue());
127 } else if (value instanceof Boolean) {
128 valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
129 (Boolean) value ? 1L : 0L);
130 } else if (value instanceof Float) {
131 valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE,
133 } else if (value instanceof String){
134 valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_VALUE,
137 Log.w(LOG_TAG, "Tried to log unloggable object" + value);
140 if (valueData != null) {
141 // Pref key exists in set, log it's change in metrics.
142 mMetricsFeature.action(mContext, MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE,
143 Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, prefKey),
148 private void logPackageName(String key, String value) {
149 final String prefKey = mTag + "/" + key;
150 mMetricsFeature.action(mContext, MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE,
151 Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, prefKey));
152 mMetricsFeature.action(mContext, MetricsEvent.ACTION_GENERIC_PACKAGE,
153 prefKey + "|" + value);
156 private void safeLogValue(String key, String value) {
157 new AsyncPackageCheck().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, key, value);
160 private class AsyncPackageCheck extends AsyncTask<String, Void, Void> {
162 protected Void doInBackground(String... params) {
163 String key = params[0];
164 String value = params[1];
165 PackageManager pm = mContext.getPackageManager();
167 // Check if this might be a component.
168 ComponentName name = ComponentName.unflattenFromString(value);
170 value = name.getPackageName();
172 } catch (Exception e) {
175 pm.getPackageInfo(value, PackageManager.MATCH_ANY_USER);
176 logPackageName(key, value);
177 } catch (PackageManager.NameNotFoundException e) {
178 // Clearly not a package, and it's unlikely this preference is in prefSet, so
179 // lets force log it.
180 logValue(key, value, true /* forceLog */);
186 public class EditorLogger implements Editor {
188 public Editor putString(String key, @Nullable String value) {
189 safeLogValue(key, value);
194 public Editor putStringSet(String key, @Nullable Set<String> values) {
195 safeLogValue(key, TextUtils.join(",", values));
200 public Editor putInt(String key, int value) {
201 logValue(key, value);
206 public Editor putLong(String key, long value) {
207 logValue(key, value);
212 public Editor putFloat(String key, float value) {
213 logValue(key, value);
218 public Editor putBoolean(String key, boolean value) {
219 logValue(key, value);
224 public Editor remove(String key) {
229 public Editor clear() {
234 public boolean commit() {
239 public void apply() {