1 /*
2  * Copyright (C) 2019 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 package com.android.tv.common.dagger.init;
17 
18 import android.content.Context;
19 import android.util.Log;
20 
21 /**
22  * Initializes objects one time only.
23  *
24  * <p>This is needed because ContentProviders can be created before Application.onCreate
25  */
26 public final class SafePreDaggerInitializer {
27     private interface Initialize {
init(Context context)28         void init(Context context);
29     }
30 
31     private static final String TAG = "SafePreDaggerInitializer";
32 
33     private static boolean initialized = false;
34     private static Context oldContext;
35 
36     private static final Initialize[] sList =
37             new Initialize[] {
38                 /* Begin_AOSP_Comment_Out
39                 com.google.android.libraries.phenotype.client.PhenotypeContext::setContext
40                 End_AOSP_Comment_Out */
41             };
42 
init(Context context)43     public static synchronized void init(Context context) {
44         if (!initialized) {
45             for (Initialize i : sList) {
46                 i.init(context);
47             }
48             oldContext = context;
49             initialized = true;
50         } else if (oldContext != context) {
51             Log.w(
52                     TAG,
53                     "init called more than once, skipping. Old context was "
54                             + oldContext
55                             + " new context is "
56                             + context);
57         }
58     }
59 
SafePreDaggerInitializer()60     private SafePreDaggerInitializer() {}
61 }
62