diff options
Diffstat (limited to 'java/com/android/dialer/configprovider')
6 files changed, 309 insertions, 0 deletions
diff --git a/java/com/android/dialer/configprovider/AndroidManifest.xml b/java/com/android/dialer/configprovider/AndroidManifest.xml new file mode 100644 index 000000000..772997153 --- /dev/null +++ b/java/com/android/dialer/configprovider/AndroidManifest.xml @@ -0,0 +1,23 @@ +<!-- + ~ Copyright (C) 2017 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License + --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.dialer.configprovider"> + + <application> + <service android:name=".SharedPrefConfigProvider$Service"/> + </application> + +</manifest>
\ No newline at end of file diff --git a/java/com/android/dialer/configprovider/ConfigProvider.java b/java/com/android/dialer/configprovider/ConfigProvider.java new file mode 100644 index 000000000..886a69e93 --- /dev/null +++ b/java/com/android/dialer/configprovider/ConfigProvider.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.configprovider; + +/** Gets config values from the container application. */ +public interface ConfigProvider { + + String getString(String key, String defaultValue); + + long getLong(String key, long defaultValue); + + boolean getBoolean(String key, boolean defaultValue); +} diff --git a/java/com/android/dialer/configprovider/ConfigProviderBindings.java b/java/com/android/dialer/configprovider/ConfigProviderBindings.java new file mode 100644 index 000000000..0bf0e758d --- /dev/null +++ b/java/com/android/dialer/configprovider/ConfigProviderBindings.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.configprovider; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.support.v4.os.UserManagerCompat; +import com.android.dialer.common.Assert; + +/** Accessor for getting a {@link ConfigProvider}. */ +public class ConfigProviderBindings { + + private static ConfigProvider configProvider; + private static ConfigProvider configProviderStub; + + public static ConfigProvider get(@NonNull Context context) { + Assert.isNotNull(context); + if (configProvider != null) { + return configProvider; + } + if (!UserManagerCompat.isUserUnlocked(context)) { + if (configProviderStub == null) { + configProviderStub = new ConfigProviderStub(); + } + return configProviderStub; + } + configProvider = ConfigProviderComponent.get(context).getConfigProvider(); + return configProvider; + } + + @VisibleForTesting + public static void setForTesting(@Nullable ConfigProvider configProviderForTesting) { + configProvider = configProviderForTesting; + } + + private static class ConfigProviderStub implements ConfigProvider { + @Override + public String getString(String key, String defaultValue) { + return defaultValue; + } + + @Override + public long getLong(String key, long defaultValue) { + return defaultValue; + } + + @Override + public boolean getBoolean(String key, boolean defaultValue) { + return defaultValue; + } + } +} diff --git a/java/com/android/dialer/configprovider/ConfigProviderComponent.java b/java/com/android/dialer/configprovider/ConfigProviderComponent.java new file mode 100644 index 000000000..e26a5cfe4 --- /dev/null +++ b/java/com/android/dialer/configprovider/ConfigProviderComponent.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.configprovider; + +import android.content.Context; +import android.support.annotation.NonNull; +import com.android.dialer.inject.HasRootComponent; +import dagger.Subcomponent; + +/** Dagger component to provide a {@link ConfigProvider}. */ +@Subcomponent +public abstract class ConfigProviderComponent { + + @NonNull + public abstract ConfigProvider getConfigProvider(); + + public static ConfigProviderComponent get(Context context) { + return ((ConfigProviderComponent.HasComponent) + ((HasRootComponent) context.getApplicationContext()).component()) + .configProviderComponent(); + } + + /** Used to refer to the root application component. */ + public interface HasComponent { + ConfigProviderComponent configProviderComponent(); + } +} diff --git a/java/com/android/dialer/configprovider/SharedPrefConfigProvider.java b/java/com/android/dialer/configprovider/SharedPrefConfigProvider.java new file mode 100644 index 000000000..6ee469572 --- /dev/null +++ b/java/com/android/dialer/configprovider/SharedPrefConfigProvider.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.android.dialer.configprovider; + +import android.app.IntentService; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.support.annotation.Nullable; +import com.android.dialer.common.Assert; +import com.android.dialer.common.LogUtil; +import com.android.dialer.inject.ApplicationContext; +import com.android.dialer.strictmode.DialerStrictMode; +import com.android.dialer.util.DialerUtils; +import javax.inject.Inject; + +/** + * {@link ConfigProvider} which uses a shared preferences file. + * + * <p>Config flags can be written using adb (with root access), for example: + * + * <pre> + * adb root + * adb shell am startservice -n \ + * 'com.android.dialer/.configprovider.SharedPrefConfigProvider\$Service' \ + * --ez boolean_flag_name flag_value + * </pre> + * + * <p>(For longs use --el and for strings use --es.) + * + * <p>Flags can be viewed with: + * + * <pre> + * adb shell cat \ + * /data/user_de/0/com.android.dialer/shared_prefs/com.android.dialer_preferences.xml + * </pre> + */ +class SharedPrefConfigProvider implements ConfigProvider { + private static final String PREF_PREFIX = "config_provider_prefs_"; + + private final Context appContext; + + @Inject + SharedPrefConfigProvider(@ApplicationContext Context appContext) { + this.appContext = appContext; + } + + /** Service to write values into {@link SharedPrefConfigProvider} using adb. */ + public static class Service extends IntentService { + + public Service() { + super("SharedPrefConfigProvider.Service"); + } + + @Override + protected void onHandleIntent(@Nullable Intent intent) { + if (intent == null || intent.getExtras() == null || intent.getExtras().size() != 1) { + LogUtil.w("SharedPrefConfigProvider.Service.onHandleIntent", "must set exactly one extra"); + return; + } + String key = intent.getExtras().keySet().iterator().next(); + Object value = intent.getExtras().get(key); + put(key, value); + } + + private void put(String key, Object value) { + Editor editor = getSharedPrefs(getApplicationContext()).edit(); + String prefixedKey = PREF_PREFIX + key; + if (value instanceof Boolean) { + editor.putBoolean(prefixedKey, (Boolean) value); + } else if (value instanceof Long) { + editor.putLong(prefixedKey, (Long) value); + } else if (value instanceof String) { + editor.putString(prefixedKey, (String) value); + } else { + throw Assert.createAssertionFailException("unsupported extra type: " + value.getClass()); + } + editor.apply(); + } + } + + @Override + public String getString(String key, String defaultValue) { + // Reading shared prefs on the main thread is generally safe since a single instance is cached. + return DialerStrictMode.bypass( + () -> getSharedPrefs(appContext).getString(PREF_PREFIX + key, defaultValue)); + } + + @Override + public long getLong(String key, long defaultValue) { + // Reading shared prefs on the main thread is generally safe since a single instance is cached. + return DialerStrictMode.bypass( + () -> getSharedPrefs(appContext).getLong(PREF_PREFIX + key, defaultValue)); + } + + @Override + public boolean getBoolean(String key, boolean defaultValue) { + // Reading shared prefs on the main thread is generally safe since a single instance is cached. + return DialerStrictMode.bypass( + () -> getSharedPrefs(appContext).getBoolean(PREF_PREFIX + key, defaultValue)); + } + + private static SharedPreferences getSharedPrefs(Context appContext) { + return DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(appContext); + } +} diff --git a/java/com/android/dialer/configprovider/SharedPrefConfigProviderModule.java b/java/com/android/dialer/configprovider/SharedPrefConfigProviderModule.java new file mode 100644 index 000000000..fe1c90816 --- /dev/null +++ b/java/com/android/dialer/configprovider/SharedPrefConfigProviderModule.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.configprovider; + +import dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +/** Dagger module providing {@link ConfigProvider} based on shared preferences. */ +@Module +public abstract class SharedPrefConfigProviderModule { + + @Binds + @Singleton + abstract ConfigProvider to(SharedPrefConfigProvider impl); +} |