blob: 900544220ccecd9d51813010097a5c8ac4ddce8b [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;
27import android.graphics.Point;
Jeff Sharkey8b997042013-09-19 15:25:56 -070028import android.net.Uri;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070029import android.os.RemoteException;
30import android.text.format.DateUtils;
Jeff Sharkey873daa32013-08-18 17:38:20 -070031
32public class DocumentsApplication extends Application {
Jeff Sharkey3fd11772013-09-30 14:26:27 -070033 private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS;
34
Jeff Sharkey873daa32013-08-18 17:38:20 -070035 private RootsCache mRoots;
36 private Point mThumbnailsSize;
37 private ThumbnailCache mThumbnails;
38
39 public static RootsCache getRootsCache(Context context) {
40 return ((DocumentsApplication) context.getApplicationContext()).mRoots;
41 }
42
43 public static ThumbnailCache getThumbnailsCache(Context context, Point size) {
44 final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext();
45 final ThumbnailCache thumbnails = app.mThumbnails;
46 if (!size.equals(app.mThumbnailsSize)) {
47 thumbnails.evictAll();
48 app.mThumbnailsSize = size;
49 }
50 return thumbnails;
51 }
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
Jeff Sharkey873daa32013-08-18 17:38:20 -070064 @Override
65 public void onCreate() {
Ben Kwaf4b0ff62016-02-02 12:11:10 -080066 super.onCreate();
67
Jeff Sharkey873daa32013-08-18 17:38:20 -070068 final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
69 final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
70
71 mRoots = new RootsCache(this);
Jeff Sharkey8b997042013-09-19 15:25:56 -070072 mRoots.updateAsync();
73
Jeff Sharkey873daa32013-08-18 17:38:20 -070074 mThumbnails = new ThumbnailCache(memoryClassBytes / 4);
75
76 final IntentFilter packageFilter = new IntentFilter();
77 packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
78 packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
79 packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Jeff Sharkey59168772013-10-23 09:59:06 -070080 packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
Jeff Sharkey873daa32013-08-18 17:38:20 -070081 packageFilter.addDataScheme("package");
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070082 registerReceiver(mCacheReceiver, packageFilter);
83
84 final IntentFilter localeFilter = new IntentFilter();
85 localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
86 registerReceiver(mCacheReceiver, localeFilter);
Jeff Sharkey873daa32013-08-18 17:38:20 -070087 }
88
89 @Override
90 public void onTrimMemory(int level) {
91 super.onTrimMemory(level);
92
93 if (level >= TRIM_MEMORY_MODERATE) {
94 mThumbnails.evictAll();
95 } else if (level >= TRIM_MEMORY_BACKGROUND) {
96 mThumbnails.trimToSize(mThumbnails.size() / 2);
97 }
98 }
99
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700100 private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700101 @Override
102 public void onReceive(Context context, Intent intent) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700103 final Uri data = intent.getData();
104 if (data != null) {
105 final String packageName = data.getSchemeSpecificPart();
106 mRoots.updatePackageAsync(packageName);
107 } else {
108 mRoots.updateAsync();
109 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700110 }
111 };
112}