blob: e0b25599184f158b5e21fa3d8ca0f8301dda3f04 [file] [log] [blame]
Jeff Sharkey873daa32013-08-18 17:38:20 -07001/*
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
17package com.android.documentsui;
18
19import android.app.ActivityManager;
20import android.app.Application;
21import android.content.BroadcastReceiver;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070022import android.content.ContentProviderClient;
23import android.content.ContentResolver;
Jeff Sharkey873daa32013-08-18 17:38:20 -070024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Jeff Sharkey8b997042013-09-19 15:25:56 -070027import android.net.Uri;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070028import android.os.RemoteException;
29import android.text.format.DateUtils;
Jeff Sharkey873daa32013-08-18 17:38:20 -070030
Steve McKay988d8a32016-09-27 09:41:17 -070031import com.android.documentsui.clipping.ClipStorage;
Steve McKayc8889af2016-09-23 11:22:41 -070032import com.android.documentsui.clipping.ClipStore;
Garfield, Tan9666ce62016-07-12 11:02:09 -070033import com.android.documentsui.clipping.DocumentClipper;
Jon Mann9bd40992017-03-24 12:34:34 -070034import com.android.documentsui.roots.ProvidersCache;
Garfield, Tan9666ce62016-07-12 11:02:09 -070035
Jeff Sharkey873daa32013-08-18 17:38:20 -070036public class DocumentsApplication extends Application {
Jeff Sharkey3fd11772013-09-30 14:26:27 -070037 private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS;
38
Jon Mann9bd40992017-03-24 12:34:34 -070039 private ProvidersCache mProviders;
Garfield, Tanc8099c02016-05-02 12:01:30 -070040 private ThumbnailCache mThumbnailCache;
Steve McKayc8889af2016-09-23 11:22:41 -070041 private ClipStorage mClipStore;
Garfield, Tan4d009142016-05-12 14:12:18 -070042 private DocumentClipper mClipper;
Garfield Tanda2c0f02017-04-11 13:47:58 -070043 private DragAndDropManager mDragAndDropManager;
Garfield, Tan4d009142016-05-12 14:12:18 -070044
Jon Mann9bd40992017-03-24 12:34:34 -070045 public static ProvidersCache getProvidersCache(Context context) {
46 return ((DocumentsApplication) context.getApplicationContext()).mProviders;
Jeff Sharkey873daa32013-08-18 17:38:20 -070047 }
48
Garfield, Tanc8099c02016-05-02 12:01:30 -070049 public static ThumbnailCache getThumbnailCache(Context context) {
Jeff Sharkey873daa32013-08-18 17:38:20 -070050 final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext();
Garfield, Tanc8099c02016-05-02 12:01:30 -070051 return app.mThumbnailCache;
Jeff Sharkey873daa32013-08-18 17:38:20 -070052 }
53
Jeff Sharkey3fd11772013-09-30 14:26:27 -070054 public static ContentProviderClient acquireUnstableProviderOrThrow(
55 ContentResolver resolver, String authority) throws RemoteException {
56 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
57 authority);
58 if (client == null) {
59 throw new RemoteException("Failed to acquire provider for " + authority);
60 }
61 client.setDetectNotResponding(PROVIDER_ANR_TIMEOUT);
62 return client;
63 }
64
Garfield, Tan4d009142016-05-12 14:12:18 -070065 public static DocumentClipper getDocumentClipper(Context context) {
66 return ((DocumentsApplication) context.getApplicationContext()).mClipper;
67 }
68
Steve McKayc8889af2016-09-23 11:22:41 -070069 public static ClipStore getClipStore(Context context) {
70 return ((DocumentsApplication) context.getApplicationContext()).mClipStore;
Garfield, Tanedce5542016-06-17 15:32:28 -070071 }
72
Garfield Tanda2c0f02017-04-11 13:47:58 -070073 public static DragAndDropManager getDragAndDropManager(Context context) {
74 return ((DocumentsApplication) context.getApplicationContext()).mDragAndDropManager;
75 }
76
Jeff Sharkey873daa32013-08-18 17:38:20 -070077 @Override
78 public void onCreate() {
Ben Kwaf4b0ff62016-02-02 12:11:10 -080079 super.onCreate();
80
Jeff Sharkey873daa32013-08-18 17:38:20 -070081 final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
82 final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
83
Jon Mann9bd40992017-03-24 12:34:34 -070084 mProviders = new ProvidersCache(this);
85 mProviders.updateAsync(false);
Jeff Sharkey8b997042013-09-19 15:25:56 -070086
Garfield, Tanc8099c02016-05-02 12:01:30 -070087 mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4);
Jeff Sharkey873daa32013-08-18 17:38:20 -070088
Steve McKayc8889af2016-09-23 11:22:41 -070089 mClipStore = new ClipStorage(
Garfield, Tanb7e5f6b2016-06-30 18:27:47 -070090 ClipStorage.prepareStorage(getCacheDir()),
91 getSharedPreferences(ClipStorage.PREF_NAME, 0));
Ben Lin30b0dc12017-03-07 15:37:16 -080092 mClipper = DocumentClipper.create(this, mClipStore);
Garfield, Tan4d009142016-05-12 14:12:18 -070093
Garfield Tanda2c0f02017-04-11 13:47:58 -070094 mDragAndDropManager = DragAndDropManager.create(this, mClipper);
95
Jeff Sharkey873daa32013-08-18 17:38:20 -070096 final IntentFilter packageFilter = new IntentFilter();
97 packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
98 packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
99 packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Jeff Sharkey59168772013-10-23 09:59:06 -0700100 packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700101 packageFilter.addDataScheme("package");
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700102 registerReceiver(mCacheReceiver, packageFilter);
103
104 final IntentFilter localeFilter = new IntentFilter();
105 localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
106 registerReceiver(mCacheReceiver, localeFilter);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700107 }
108
109 @Override
110 public void onTrimMemory(int level) {
111 super.onTrimMemory(level);
112
Garfield, Tanc8099c02016-05-02 12:01:30 -0700113 mThumbnailCache.onTrimMemory(level);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700114 }
115
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700116 private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700117 @Override
118 public void onReceive(Context context, Intent intent) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700119 final Uri data = intent.getData();
120 if (data != null) {
121 final String packageName = data.getSchemeSpecificPart();
Jon Mann9bd40992017-03-24 12:34:34 -0700122 mProviders.updatePackageAsync(packageName);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700123 } else {
Jon Mann9bd40992017-03-24 12:34:34 -0700124 mProviders.updateAsync(true);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700125 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700126 }
127 };
128}