blob: a751539f70f4a0c09352576f31ffce90659e7b0e [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
Garfield, Tan9666ce62016-07-12 11:02:09 -070031import com.android.documentsui.clipping.ClipStorage;
32import com.android.documentsui.clipping.DocumentClipper;
Steve McKayd9caa6a2016-09-15 16:36:45 -070033import com.android.documentsui.roots.RootsCache;
Garfield, Tan9666ce62016-07-12 11:02:09 -070034
Jeff Sharkey873daa32013-08-18 17:38:20 -070035public class DocumentsApplication extends Application {
Jeff Sharkey3fd11772013-09-30 14:26:27 -070036 private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS;
37
Jeff Sharkey873daa32013-08-18 17:38:20 -070038 private RootsCache mRoots;
Garfield, Tanc8099c02016-05-02 12:01:30 -070039
40 private ThumbnailCache mThumbnailCache;
Garfield, Tanedce5542016-06-17 15:32:28 -070041 private ClipStorage mClipStorage;
Garfield, Tan4d009142016-05-12 14:12:18 -070042 private DocumentClipper mClipper;
43
Jeff Sharkey873daa32013-08-18 17:38:20 -070044 public static RootsCache getRootsCache(Context context) {
45 return ((DocumentsApplication) context.getApplicationContext()).mRoots;
46 }
47
Garfield, Tanc8099c02016-05-02 12:01:30 -070048 public static ThumbnailCache getThumbnailCache(Context context) {
Jeff Sharkey873daa32013-08-18 17:38:20 -070049 final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext();
Garfield, Tanc8099c02016-05-02 12:01:30 -070050 return app.mThumbnailCache;
Jeff Sharkey873daa32013-08-18 17:38:20 -070051 }
52
Jeff Sharkey3fd11772013-09-30 14:26:27 -070053 public static ContentProviderClient acquireUnstableProviderOrThrow(
54 ContentResolver resolver, String authority) throws RemoteException {
55 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
56 authority);
57 if (client == null) {
58 throw new RemoteException("Failed to acquire provider for " + authority);
59 }
60 client.setDetectNotResponding(PROVIDER_ANR_TIMEOUT);
61 return client;
62 }
63
Garfield, Tan4d009142016-05-12 14:12:18 -070064 public static DocumentClipper getDocumentClipper(Context context) {
65 return ((DocumentsApplication) context.getApplicationContext()).mClipper;
66 }
67
Garfield, Tanedce5542016-06-17 15:32:28 -070068 public static ClipStorage getClipStorage(Context context) {
69 return ((DocumentsApplication) context.getApplicationContext()).mClipStorage;
70 }
71
Jeff Sharkey873daa32013-08-18 17:38:20 -070072 @Override
73 public void onCreate() {
Ben Kwaf4b0ff62016-02-02 12:11:10 -080074 super.onCreate();
75
Jeff Sharkey873daa32013-08-18 17:38:20 -070076 final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
77 final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
78
79 mRoots = new RootsCache(this);
Jeff Sharkeyd330a6c2016-04-04 08:58:04 -060080 mRoots.updateAsync(false);
Jeff Sharkey8b997042013-09-19 15:25:56 -070081
Garfield, Tanc8099c02016-05-02 12:01:30 -070082 mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4);
Jeff Sharkey873daa32013-08-18 17:38:20 -070083
Garfield, Tanb7e5f6b2016-06-30 18:27:47 -070084 mClipStorage = new ClipStorage(
85 ClipStorage.prepareStorage(getCacheDir()),
86 getSharedPreferences(ClipStorage.PREF_NAME, 0));
Garfield, Tanedce5542016-06-17 15:32:28 -070087 mClipper = new DocumentClipper(this, mClipStorage);
Garfield, Tan4d009142016-05-12 14:12:18 -070088
Jeff Sharkey873daa32013-08-18 17:38:20 -070089 final IntentFilter packageFilter = new IntentFilter();
90 packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
91 packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
92 packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Jeff Sharkey59168772013-10-23 09:59:06 -070093 packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
Jeff Sharkey873daa32013-08-18 17:38:20 -070094 packageFilter.addDataScheme("package");
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070095 registerReceiver(mCacheReceiver, packageFilter);
96
97 final IntentFilter localeFilter = new IntentFilter();
98 localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
99 registerReceiver(mCacheReceiver, localeFilter);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700100 }
101
102 @Override
103 public void onTrimMemory(int level) {
104 super.onTrimMemory(level);
105
Garfield, Tanc8099c02016-05-02 12:01:30 -0700106 mThumbnailCache.onTrimMemory(level);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700107 }
108
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700109 private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700110 @Override
111 public void onReceive(Context context, Intent intent) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700112 final Uri data = intent.getData();
113 if (data != null) {
114 final String packageName = data.getSchemeSpecificPart();
115 mRoots.updatePackageAsync(packageName);
116 } else {
Jeff Sharkeyd330a6c2016-04-04 08:58:04 -0600117 mRoots.updateAsync(true);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700118 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700119 }
120 };
121}