1 package com.android.pump.app; 2 3 import android.app.Application; 4 5 import androidx.annotation.NonNull; 6 import androidx.annotation.UiThread; 7 import androidx.recyclerview.widget.RecyclerView.RecycledViewPool; 8 9 import com.android.pump.concurrent.Executors; 10 import com.android.pump.db.DataProvider; 11 import com.android.pump.db.MediaDb; 12 import com.android.pump.provider.KnowledgeGraph; 13 import com.android.pump.ui.CustomRecycledViewPool; 14 import com.android.pump.util.Globals; 15 import com.android.pump.util.ImageLoader; 16 17 import java.util.concurrent.Executor; 18 19 @UiThread 20 public abstract class GlobalsApplication extends Application implements Globals.Provider { 21 private Executor mExecutor; 22 private ImageLoader mImageLoader; 23 private RecycledViewPool mRecycledViewPool; 24 private MediaDb mMediaDb; 25 26 @Override onTrimMemory(int level)27 public void onTrimMemory(int level) { 28 super.onTrimMemory(level); 29 // TODO(b/123038906) Implement 30 } 31 32 @Override getImageLoader()33 public @NonNull ImageLoader getImageLoader() { 34 if (mImageLoader == null) { 35 mImageLoader = new ImageLoader(getContentResolver(), getExecutor()); 36 } 37 return mImageLoader; 38 } 39 40 @Override getRecycledViewPool()41 public @NonNull RecycledViewPool getRecycledViewPool() { 42 if (mRecycledViewPool == null) { 43 mRecycledViewPool = new CustomRecycledViewPool(); 44 } 45 return mRecycledViewPool; 46 } 47 48 @Override getMediaDb()49 public @NonNull MediaDb getMediaDb() { 50 if (mMediaDb == null) { 51 mMediaDb = new MediaDb(getContentResolver(), getDataProvider(), getExecutor()); 52 // TODO When can we release mMediaDb? 53 } 54 return mMediaDb; 55 } 56 getExecutor()57 private @NonNull Executor getExecutor() { 58 if (mExecutor == null) { 59 // TODO Adjust pool size 60 mExecutor = Executors.newFixedUniqueThreadPool( 61 Runtime.getRuntime().availableProcessors() * 2 + 1); 62 } 63 return mExecutor; 64 } 65 getDataProvider()66 private @NonNull DataProvider getDataProvider() { 67 return KnowledgeGraph.getInstance(); 68 } 69 } 70