1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 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 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 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. 13 */ 14 15 package com.android.settings; 16 17 import com.android.internal.logging.MetricsLogger; 18 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; 28 29 import java.util.Map; 30 import java.util.Set; 31 32 public class SharedPreferencesLogger implements SharedPreferences { 33 34 private final String mTag; 35 private final Context mContext; 36 37 public SharedPreferencesLogger(Context context, String tag) { 38 mContext = context; 39 mTag = tag; 40 } 41 42 @Override 43 public Map<String, ?> getAll() { 44 return null; 45 } 46 47 @Override 48 public String getString(String key, @Nullable String defValue) { 49 return defValue; 50 } 51 52 @Override 53 public Set<String> getStringSet(String key, @Nullable Set<String> defValues) { 54 return defValues; 55 } 56 57 @Override 58 public int getInt(String key, int defValue) { 59 return defValue; 60 } 61 62 @Override 63 public long getLong(String key, long defValue) { 64 return defValue; 65 } 66 67 @Override 68 public float getFloat(String key, float defValue) { 69 return defValue; 70 } 71 72 @Override 73 public boolean getBoolean(String key, boolean defValue) { 74 return defValue; 75 } 76 77 @Override 78 public boolean contains(String key) { 79 return false; 80 } 81 82 @Override 83 public Editor edit() { 84 return new EditorLogger(); 85 } 86 87 @Override 88 public void registerOnSharedPreferenceChangeListener( 89 OnSharedPreferenceChangeListener listener) { 90 } 91 92 @Override 93 public void unregisterOnSharedPreferenceChangeListener( 94 OnSharedPreferenceChangeListener listener) { 95 } 96 97 private void logValue(String key, String value) { 98 MetricsLogger.count(mContext, mTag + "/" + key + "|" + value, 1); 99 } 100 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); 105 } 106 107 private void safeLogValue(String key, String value) { 108 new AsyncPackageCheck().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, key, value); 109 } 110 111 private class AsyncPackageCheck extends AsyncTask<String, Void, Void> { 112 @Override 113 protected Void doInBackground(String... params) { 114 String key = params[0]; 115 String value = params[1]; 116 PackageManager pm = mContext.getPackageManager(); 117 try { 118 // Check if this might be a component. 119 ComponentName name = ComponentName.unflattenFromString(value); 120 if (value != null) { 121 value = name.getPackageName(); 122 } 123 } catch (Exception e) { 124 } 125 try { 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); 131 } 132 return null; 133 } 134 } 135 136 public class EditorLogger implements Editor { 137 @Override 138 public Editor putString(String key, @Nullable String value) { 139 safeLogValue(key, value); 140 return this; 141 } 142 143 @Override 144 public Editor putStringSet(String key, @Nullable Set<String> values) { 145 safeLogValue(key, TextUtils.join(",", values)); 146 return this; 147 } 148 149 @Override 150 public Editor putInt(String key, int value) { 151 logValue(key, String.valueOf(value)); 152 return this; 153 } 154 155 @Override 156 public Editor putLong(String key, long value) { 157 logValue(key, String.valueOf(value)); 158 return this; 159 } 160 161 @Override 162 public Editor putFloat(String key, float value) { 163 logValue(key, String.valueOf(value)); 164 return this; 165 } 166 167 @Override 168 public Editor putBoolean(String key, boolean value) { 169 logValue(key, String.valueOf(value)); 170 return this; 171 } 172 173 @Override 174 public Editor remove(String key) { 175 return this; 176 } 177 178 @Override 179 public Editor clear() { 180 return this; 181 } 182 183 @Override 184 public boolean commit() { 185 return true; 186 } 187 188 @Override 189 public void apply() { 190 } 191 } 192 } 193