1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.dialer.common;
18 
19 import android.content.Context;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.Nullable;
22 import android.support.annotation.VisibleForTesting;
23 import android.support.v4.os.UserManagerCompat;
24 
25 /** Accessor for getting a {@link ConfigProvider}. */
26 public class ConfigProviderBindings {
27 
28   private static ConfigProvider configProvider;
29   private static ConfigProvider configProviderStub;
30 
get(@onNull Context context)31   public static ConfigProvider get(@NonNull Context context) {
32     Assert.isNotNull(context);
33     if (configProvider != null) {
34       return configProvider;
35     }
36     if (!UserManagerCompat.isUserUnlocked(context)) {
37       if (configProviderStub == null) {
38         configProviderStub = new ConfigProviderStub();
39       }
40       return configProviderStub;
41     }
42 
43     Context application = context.getApplicationContext();
44     if (application instanceof ConfigProviderFactory) {
45       configProvider = ((ConfigProviderFactory) application).getConfigProvider();
46     }
47 
48     if (configProvider == null) {
49       configProvider = new ConfigProviderStub();
50     }
51 
52     return configProvider;
53   }
54 
55   @VisibleForTesting
setForTesting(@ullable ConfigProvider configProviderForTesting)56   public static void setForTesting(@Nullable ConfigProvider configProviderForTesting) {
57     configProvider = configProviderForTesting;
58   }
59 
60   private static class ConfigProviderStub implements ConfigProvider {
61     @Override
getString(String key, String defaultValue)62     public String getString(String key, String defaultValue) {
63       return defaultValue;
64     }
65 
66     @Override
getLong(String key, long defaultValue)67     public long getLong(String key, long defaultValue) {
68       return defaultValue;
69     }
70 
71     @Override
getBoolean(String key, boolean defaultValue)72     public boolean getBoolean(String key, boolean defaultValue) {
73       return defaultValue;
74     }
75   }
76 }
77