1 /* 2 * Copyright (C) 2020 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.car.messenger.core.interfaces; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 22 import androidx.annotation.NonNull; 23 24 /** 25 * The AppFactory provides singleton instances to be used throughout the app. 26 * 27 * <p>The Factory implementation points to the UI core library interfaces. 28 * 29 * <p>Once the Factory implementation is initialized with {@link #setInstance(AppFactory)}, the 30 * library interface implementations can be accessed anywhere throughout the application. 31 */ 32 public abstract class AppFactory { 33 @NonNull private static AppFactory sInstance; 34 protected static boolean sRegistered; 35 protected static boolean sInitialized; 36 37 /** Returns the Factory instance for the Application. */ 38 @NonNull get()39 public static AppFactory get() { 40 return sInstance; 41 } 42 43 /** 44 * Sets the Factory instance. 45 * 46 * <p>This is called when the application starts, in onCreate of the custom Application class 47 */ setInstance(@onNull final AppFactory factory)48 protected static void setInstance(@NonNull final AppFactory factory) { 49 // Not allowed to call this after real application initialization is complete 50 if (sRegistered && sInitialized) { 51 return; 52 } 53 sInstance = factory; 54 } 55 56 /** Returns context most appropriate for UI context-requiring tasks. */ 57 @NonNull getContext()58 public abstract Context getContext(); 59 60 /** 61 * Perhaps the single most important methods to implement, this provides the data source for the 62 * app. 63 */ 64 @NonNull getDataModel()65 public abstract DataModel getDataModel(); 66 67 /** Returns the shared preference instance for the app */ 68 @NonNull getSharedPreferences()69 public abstract SharedPreferences getSharedPreferences(); 70 } 71