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