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