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
17package com.android.incallui;
18
19import android.content.Context;
20import android.support.annotation.Nullable;
21import android.support.v4.os.UserManagerCompat;
22import com.android.contacts.common.preference.ContactsPreferences;
23
24/** Factory class for {@link ContactsPreferences}. */
25public class ContactsPreferencesFactory {
26
27  private static boolean sUseTestInstance;
28  private static ContactsPreferences sTestInstance;
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 (sUseTestInstance) {
39      return sTestInstance;
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    sUseTestInstance = true;
54    sTestInstance = testInstance;
55  }
56}
57