Home | History | Annotate | Download | only in incallui
      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.incallui;
     18 
     19 import android.content.Context;
     20 import android.support.annotation.Nullable;
     21 import com.android.dialer.compat.UserManagerCompat;
     22 
     23 import com.android.contacts.common.preference.ContactsPreferences;
     24 import com.android.contacts.common.testing.NeededForTesting;
     25 
     26 /**
     27  * Factory class for {@link ContactsPreferences}.
     28  */
     29 public class ContactsPreferencesFactory {
     30 
     31     private static boolean sUseTestInstance;
     32     private static ContactsPreferences sTestInstance;
     33 
     34     /**
     35      * Creates a new {@link ContactsPreferences} object if possible.
     36      *
     37      * @param context the context to use when creating the ContactsPreferences.
     38      * @return a new ContactsPreferences object or {@code null} if the user is locked.
     39      */
     40     @Nullable
     41     public static ContactsPreferences newContactsPreferences(Context context) {
     42         if (sUseTestInstance) {
     43             return sTestInstance;
     44         }
     45         if (UserManagerCompat.isUserUnlocked(context)) {
     46             return new ContactsPreferences(context);
     47         }
     48         return null;
     49     }
     50 
     51     /**
     52      * Sets the instance to be returned by all calls to {@link #newContactsPreferences(Context)}.
     53      *
     54      * @param testInstance the instance to return.
     55      */
     56     @NeededForTesting
     57     static void setTestInstance(ContactsPreferences testInstance) {
     58         sUseTestInstance = true;
     59         sTestInstance = testInstance;
     60     }
     61 }
     62