1 /* 2 * Copyright 2018 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.pump.util; 18 19 import android.content.Context; 20 import android.content.ContextWrapper; 21 22 import androidx.annotation.AnyThread; 23 import androidx.annotation.NonNull; 24 import androidx.recyclerview.widget.RecyclerView.RecycledViewPool; 25 26 import com.android.pump.db.MediaDb; 27 28 @AnyThread 29 public final class Globals { Globals()30 private Globals() { } 31 getImageLoader(@onNull Context context)32 public static @NonNull ImageLoader getImageLoader(@NonNull Context context) { 33 return getProvider(context).getImageLoader(); 34 } 35 getRecycledViewPool(@onNull Context context)36 public static @NonNull RecycledViewPool getRecycledViewPool(@NonNull Context context) { 37 return getProvider(context).getRecycledViewPool(); 38 } 39 getMediaDb(@onNull Context context)40 public static @NonNull MediaDb getMediaDb(@NonNull Context context) { 41 return getProvider(context).getMediaDb(); 42 } 43 44 public interface Provider { getImageLoader()45 @NonNull ImageLoader getImageLoader(); getRecycledViewPool()46 @NonNull RecycledViewPool getRecycledViewPool(); getMediaDb()47 @NonNull MediaDb getMediaDb(); 48 } 49 getProvider(@onNull Context context)50 private static @NonNull Provider getProvider(@NonNull Context context) { 51 while (!(context instanceof Provider)) { 52 if (context instanceof ContextWrapper) { 53 context = ((ContextWrapper) context).getBaseContext(); 54 } else { 55 context = context.getApplicationContext(); 56 if (!(context instanceof Provider)) { 57 throw new IllegalArgumentException("No global provider in context"); 58 } 59 } 60 } 61 return (Provider) context; 62 } 63 } 64