blob: 65204b7e65bd7e3a7166020c910bd0b62f010f74 [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;
Bill Lin3c78f552019-04-22 20:26:38 +080027import android.content.om.OverlayManager;
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;
Bill Lin3c78f552019-04-22 20:26:38 +080031import android.util.Log;
Jeff Sharkey873daa32013-08-18 17:38:20 -070032
Garfield Tanb47b4b52017-05-16 12:48:53 -070033import com.android.documentsui.base.Lookup;
Steve McKay988d8a32016-09-27 09:41:17 -070034import com.android.documentsui.clipping.ClipStorage;
Steve McKayc8889af2016-09-23 11:22:41 -070035import com.android.documentsui.clipping.ClipStore;
Garfield, Tan9666ce62016-07-12 11:02:09 -070036import com.android.documentsui.clipping.DocumentClipper;
Amin Shaikhced7a452018-10-23 14:22:48 -040037import com.android.documentsui.prefs.ScopedAccessLocalPreferences;
Tony Huang2a022ba2019-01-10 20:02:52 +080038import com.android.documentsui.queries.SearchHistoryManager;
Jon Mann9bd40992017-03-24 12:34:34 -070039import com.android.documentsui.roots.ProvidersCache;
Bill Lin3c78f552019-04-22 20:26:38 +080040import com.android.documentsui.theme.ThemeOverlayManager;
Garfield, Tan9666ce62016-07-12 11:02:09 -070041
Jeff Sharkey873daa32013-08-18 17:38:20 -070042public class DocumentsApplication extends Application {
Bill Lin3c78f552019-04-22 20:26:38 +080043 private static final String TAG = "DocumentsApplication";
Jeff Sharkey3fd11772013-09-30 14:26:27 -070044 private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS;
45
Jon Mann9bd40992017-03-24 12:34:34 -070046 private ProvidersCache mProviders;
Garfield, Tanc8099c02016-05-02 12:01:30 -070047 private ThumbnailCache mThumbnailCache;
Steve McKayc8889af2016-09-23 11:22:41 -070048 private ClipStorage mClipStore;
Garfield, Tan4d009142016-05-12 14:12:18 -070049 private DocumentClipper mClipper;
Garfield Tanda2c0f02017-04-11 13:47:58 -070050 private DragAndDropManager mDragAndDropManager;
Garfield Tanb47b4b52017-05-16 12:48:53 -070051 private Lookup<String, String> mFileTypeLookup;
Garfield, Tan4d009142016-05-12 14:12:18 -070052
Jon Mann9bd40992017-03-24 12:34:34 -070053 public static ProvidersCache getProvidersCache(Context context) {
54 return ((DocumentsApplication) context.getApplicationContext()).mProviders;
Jeff Sharkey873daa32013-08-18 17:38:20 -070055 }
56
Garfield, Tanc8099c02016-05-02 12:01:30 -070057 public static ThumbnailCache getThumbnailCache(Context context) {
Jeff Sharkey873daa32013-08-18 17:38:20 -070058 final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext();
Garfield, Tanc8099c02016-05-02 12:01:30 -070059 return app.mThumbnailCache;
Jeff Sharkey873daa32013-08-18 17:38:20 -070060 }
61
Jeff Sharkey3fd11772013-09-30 14:26:27 -070062 public static ContentProviderClient acquireUnstableProviderOrThrow(
63 ContentResolver resolver, String authority) throws RemoteException {
64 final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
65 authority);
66 if (client == null) {
67 throw new RemoteException("Failed to acquire provider for " + authority);
68 }
69 client.setDetectNotResponding(PROVIDER_ANR_TIMEOUT);
70 return client;
71 }
72
Garfield, Tan4d009142016-05-12 14:12:18 -070073 public static DocumentClipper getDocumentClipper(Context context) {
74 return ((DocumentsApplication) context.getApplicationContext()).mClipper;
75 }
76
Steve McKayc8889af2016-09-23 11:22:41 -070077 public static ClipStore getClipStore(Context context) {
78 return ((DocumentsApplication) context.getApplicationContext()).mClipStore;
Garfield, Tanedce5542016-06-17 15:32:28 -070079 }
80
Garfield Tanda2c0f02017-04-11 13:47:58 -070081 public static DragAndDropManager getDragAndDropManager(Context context) {
82 return ((DocumentsApplication) context.getApplicationContext()).mDragAndDropManager;
83 }
84
Garfield Tanb47b4b52017-05-16 12:48:53 -070085 public static Lookup<String, String> getFileTypeLookup(Context context) {
86 return ((DocumentsApplication) context.getApplicationContext()).mFileTypeLookup;
87 }
88
Bill Lin3c78f552019-04-22 20:26:38 +080089 private void onApplyOverlayFinish(boolean result) {
90 Log.d(TAG, "OverlayManager.setEnabled() result: " + result);
91 }
92
Jeff Sharkey873daa32013-08-18 17:38:20 -070093 @Override
94 public void onCreate() {
Ben Kwaf4b0ff62016-02-02 12:11:10 -080095 super.onCreate();
96
Jeff Sharkey873daa32013-08-18 17:38:20 -070097 final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Bill Lin3c78f552019-04-22 20:26:38 +080098 final OverlayManager om = getSystemService(OverlayManager.class);
Jeff Sharkey873daa32013-08-18 17:38:20 -070099 final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
100
Bill Lin3c78f552019-04-22 20:26:38 +0800101 if (om != null) {
102 new ThemeOverlayManager(om, getPackageName()).applyOverlays(this, true,
103 this::onApplyOverlayFinish);
104 } else {
105 Log.w(TAG, "Can't obtain OverlayManager from System Service!");
106 }
107
Jon Mann9bd40992017-03-24 12:34:34 -0700108 mProviders = new ProvidersCache(this);
109 mProviders.updateAsync(false);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700110
Garfield, Tanc8099c02016-05-02 12:01:30 -0700111 mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700112
Steve McKayc8889af2016-09-23 11:22:41 -0700113 mClipStore = new ClipStorage(
Garfield, Tanb7e5f6b2016-06-30 18:27:47 -0700114 ClipStorage.prepareStorage(getCacheDir()),
115 getSharedPreferences(ClipStorage.PREF_NAME, 0));
Ben Lin30b0dc12017-03-07 15:37:16 -0800116 mClipper = DocumentClipper.create(this, mClipStore);
Garfield, Tan4d009142016-05-12 14:12:18 -0700117
Garfield Tanda2c0f02017-04-11 13:47:58 -0700118 mDragAndDropManager = DragAndDropManager.create(this, mClipper);
119
Garfield Tanb47b4b52017-05-16 12:48:53 -0700120 mFileTypeLookup = new FileTypeMap(this);
121
Jeff Sharkey873daa32013-08-18 17:38:20 -0700122 final IntentFilter packageFilter = new IntentFilter();
123 packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
124 packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
125 packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Jeff Sharkey59168772013-10-23 09:59:06 -0700126 packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700127 packageFilter.addDataScheme("package");
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700128 registerReceiver(mCacheReceiver, packageFilter);
129
130 final IntentFilter localeFilter = new IntentFilter();
131 localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
132 registerReceiver(mCacheReceiver, localeFilter);
Amin Shaikhced7a452018-10-23 14:22:48 -0400133 ScopedAccessLocalPreferences.clearScopedAccessPreferences(this);
Tony Huang2a022ba2019-01-10 20:02:52 +0800134
135 SearchHistoryManager.getInstance(getApplicationContext());
Jeff Sharkey873daa32013-08-18 17:38:20 -0700136 }
137
138 @Override
139 public void onTrimMemory(int level) {
140 super.onTrimMemory(level);
141
Garfield, Tanc8099c02016-05-02 12:01:30 -0700142 mThumbnailCache.onTrimMemory(level);
Jeff Sharkey873daa32013-08-18 17:38:20 -0700143 }
144
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700145 private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() {
Jeff Sharkey873daa32013-08-18 17:38:20 -0700146 @Override
147 public void onReceive(Context context, Intent intent) {
Jeff Sharkey8b997042013-09-19 15:25:56 -0700148 final Uri data = intent.getData();
149 if (data != null) {
150 final String packageName = data.getSchemeSpecificPart();
Jon Mann9bd40992017-03-24 12:34:34 -0700151 mProviders.updatePackageAsync(packageName);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700152 } else {
Jon Mann9bd40992017-03-24 12:34:34 -0700153 mProviders.updateAsync(true);
Jeff Sharkey8b997042013-09-19 15:25:56 -0700154 }
Jeff Sharkey873daa32013-08-18 17:38:20 -0700155 }
156 };
157}