• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.documentsui;
18 
19 import android.app.ActivityManager;
20 import android.app.Application;
21 import android.content.BroadcastReceiver;
22 import android.content.ContentProviderClient;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.net.Uri;
28 import android.os.RemoteException;
29 import android.text.format.DateUtils;
30 
31 import com.android.documentsui.base.Lookup;
32 import com.android.documentsui.clipping.ClipStorage;
33 import com.android.documentsui.clipping.ClipStore;
34 import com.android.documentsui.clipping.DocumentClipper;
35 import com.android.documentsui.roots.ProvidersCache;
36 
37 public class DocumentsApplication extends Application {
38     private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS;
39 
40     private ProvidersCache mProviders;
41     private ThumbnailCache mThumbnailCache;
42     private ClipStorage mClipStore;
43     private DocumentClipper mClipper;
44     private DragAndDropManager mDragAndDropManager;
45     private Lookup<String, String> mFileTypeLookup;
46 
getProvidersCache(Context context)47     public static ProvidersCache getProvidersCache(Context context) {
48         return ((DocumentsApplication) context.getApplicationContext()).mProviders;
49     }
50 
getThumbnailCache(Context context)51     public static ThumbnailCache getThumbnailCache(Context context) {
52         final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext();
53         return app.mThumbnailCache;
54     }
55 
acquireUnstableProviderOrThrow( ContentResolver resolver, String authority)56     public static ContentProviderClient acquireUnstableProviderOrThrow(
57             ContentResolver resolver, String authority) throws RemoteException {
58         final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
59                 authority);
60         if (client == null) {
61             throw new RemoteException("Failed to acquire provider for " + authority);
62         }
63         client.setDetectNotResponding(PROVIDER_ANR_TIMEOUT);
64         return client;
65     }
66 
getDocumentClipper(Context context)67     public static DocumentClipper getDocumentClipper(Context context) {
68         return ((DocumentsApplication) context.getApplicationContext()).mClipper;
69     }
70 
getClipStore(Context context)71     public static ClipStore getClipStore(Context context) {
72         return ((DocumentsApplication) context.getApplicationContext()).mClipStore;
73     }
74 
getDragAndDropManager(Context context)75     public static DragAndDropManager getDragAndDropManager(Context context) {
76         return ((DocumentsApplication) context.getApplicationContext()).mDragAndDropManager;
77     }
78 
getFileTypeLookup(Context context)79     public static Lookup<String, String> getFileTypeLookup(Context context) {
80         return ((DocumentsApplication) context.getApplicationContext()).mFileTypeLookup;
81     }
82 
83     @Override
onCreate()84     public void onCreate() {
85         super.onCreate();
86 
87         final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
88         final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
89 
90         mProviders = new ProvidersCache(this);
91         mProviders.updateAsync(false);
92 
93         mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4);
94 
95         mClipStore = new ClipStorage(
96                 ClipStorage.prepareStorage(getCacheDir()),
97                 getSharedPreferences(ClipStorage.PREF_NAME, 0));
98         mClipper = DocumentClipper.create(this, mClipStore);
99 
100         mDragAndDropManager = DragAndDropManager.create(this, mClipper);
101 
102         mFileTypeLookup = new FileTypeMap(this);
103 
104         final IntentFilter packageFilter = new IntentFilter();
105         packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
106         packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
107         packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
108         packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
109         packageFilter.addDataScheme("package");
110         registerReceiver(mCacheReceiver, packageFilter);
111 
112         final IntentFilter localeFilter = new IntentFilter();
113         localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
114         registerReceiver(mCacheReceiver, localeFilter);
115     }
116 
117     @Override
onTrimMemory(int level)118     public void onTrimMemory(int level) {
119         super.onTrimMemory(level);
120 
121         mThumbnailCache.onTrimMemory(level);
122     }
123 
124     private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() {
125         @Override
126         public void onReceive(Context context, Intent intent) {
127             final Uri data = intent.getData();
128             if (data != null) {
129                 final String packageName = data.getSchemeSpecificPart();
130                 mProviders.updatePackageAsync(packageName);
131             } else {
132                 mProviders.updateAsync(true);
133             }
134         }
135     };
136 }
137