Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 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; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 22 | import android.content.ContentProviderClient; |
| 23 | import android.content.ContentResolver; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 27 | import android.content.om.OverlayManager; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 28 | import android.net.Uri; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.text.format.DateUtils; |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 31 | import android.util.Log; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 32 | |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 33 | import com.android.documentsui.base.Lookup; |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 34 | import com.android.documentsui.clipping.ClipStorage; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 35 | import com.android.documentsui.clipping.ClipStore; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 36 | import com.android.documentsui.clipping.DocumentClipper; |
Amin Shaikh | ced7a45 | 2018-10-23 14:22:48 -0400 | [diff] [blame] | 37 | import com.android.documentsui.prefs.ScopedAccessLocalPreferences; |
Tony Huang | 2a022ba | 2019-01-10 20:02:52 +0800 | [diff] [blame] | 38 | import com.android.documentsui.queries.SearchHistoryManager; |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 39 | import com.android.documentsui.roots.ProvidersCache; |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 40 | import com.android.documentsui.theme.ThemeOverlayManager; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 41 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 42 | public class DocumentsApplication extends Application { |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 43 | private static final String TAG = "DocumentsApplication"; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 44 | private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS; |
| 45 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 46 | private ProvidersCache mProviders; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 47 | private ThumbnailCache mThumbnailCache; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 48 | private ClipStorage mClipStore; |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 49 | private DocumentClipper mClipper; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 50 | private DragAndDropManager mDragAndDropManager; |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 51 | private Lookup<String, String> mFileTypeLookup; |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 52 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 53 | public static ProvidersCache getProvidersCache(Context context) { |
| 54 | return ((DocumentsApplication) context.getApplicationContext()).mProviders; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 57 | public static ThumbnailCache getThumbnailCache(Context context) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 58 | final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext(); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 59 | return app.mThumbnailCache; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 62 | 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, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 73 | public static DocumentClipper getDocumentClipper(Context context) { |
| 74 | return ((DocumentsApplication) context.getApplicationContext()).mClipper; |
| 75 | } |
| 76 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 77 | public static ClipStore getClipStore(Context context) { |
| 78 | return ((DocumentsApplication) context.getApplicationContext()).mClipStore; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 81 | public static DragAndDropManager getDragAndDropManager(Context context) { |
| 82 | return ((DocumentsApplication) context.getApplicationContext()).mDragAndDropManager; |
| 83 | } |
| 84 | |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 85 | public static Lookup<String, String> getFileTypeLookup(Context context) { |
| 86 | return ((DocumentsApplication) context.getApplicationContext()).mFileTypeLookup; |
| 87 | } |
| 88 | |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 89 | private void onApplyOverlayFinish(boolean result) { |
| 90 | Log.d(TAG, "OverlayManager.setEnabled() result: " + result); |
| 91 | } |
| 92 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public void onCreate() { |
Ben Kwa | f4b0ff6 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 95 | super.onCreate(); |
| 96 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 97 | final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 98 | final OverlayManager om = getSystemService(OverlayManager.class); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 99 | final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024; |
| 100 | |
Bill Lin | 3c78f55 | 2019-04-22 20:26:38 +0800 | [diff] [blame] | 101 | 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 Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 108 | mProviders = new ProvidersCache(this); |
| 109 | mProviders.updateAsync(false); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 110 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 111 | mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 112 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 113 | mClipStore = new ClipStorage( |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 114 | ClipStorage.prepareStorage(getCacheDir()), |
| 115 | getSharedPreferences(ClipStorage.PREF_NAME, 0)); |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 116 | mClipper = DocumentClipper.create(this, mClipStore); |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 117 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 118 | mDragAndDropManager = DragAndDropManager.create(this, mClipper); |
| 119 | |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 120 | mFileTypeLookup = new FileTypeMap(this); |
| 121 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 122 | 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 Sharkey | 5916877 | 2013-10-23 09:59:06 -0700 | [diff] [blame] | 126 | packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 127 | packageFilter.addDataScheme("package"); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 128 | registerReceiver(mCacheReceiver, packageFilter); |
| 129 | |
| 130 | final IntentFilter localeFilter = new IntentFilter(); |
| 131 | localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 132 | registerReceiver(mCacheReceiver, localeFilter); |
Amin Shaikh | ced7a45 | 2018-10-23 14:22:48 -0400 | [diff] [blame] | 133 | ScopedAccessLocalPreferences.clearScopedAccessPreferences(this); |
Tony Huang | 2a022ba | 2019-01-10 20:02:52 +0800 | [diff] [blame] | 134 | |
| 135 | SearchHistoryManager.getInstance(getApplicationContext()); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onTrimMemory(int level) { |
| 140 | super.onTrimMemory(level); |
| 141 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 142 | mThumbnailCache.onTrimMemory(level); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 145 | private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 148 | final Uri data = intent.getData(); |
| 149 | if (data != null) { |
| 150 | final String packageName = data.getSchemeSpecificPart(); |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 151 | mProviders.updatePackageAsync(packageName); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 152 | } else { |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 153 | mProviders.updateAsync(true); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 154 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | } |