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; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.text.format.DateUtils; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 30 | |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 31 | import com.android.documentsui.clipping.ClipStorage; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 32 | import com.android.documentsui.clipping.ClipStore; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 33 | import com.android.documentsui.clipping.DocumentClipper; |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 34 | import com.android.documentsui.roots.ProvidersCache; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 36 | public class DocumentsApplication extends Application { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 37 | private static final long PROVIDER_ANR_TIMEOUT = 20 * DateUtils.SECOND_IN_MILLIS; |
| 38 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 39 | private ProvidersCache mProviders; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 40 | private ThumbnailCache mThumbnailCache; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 41 | private ClipStorage mClipStore; |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 42 | private DocumentClipper mClipper; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 43 | private DragAndDropManager mDragAndDropManager; |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 44 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 45 | public static ProvidersCache getProvidersCache(Context context) { |
| 46 | return ((DocumentsApplication) context.getApplicationContext()).mProviders; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 49 | public static ThumbnailCache getThumbnailCache(Context context) { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 50 | final DocumentsApplication app = (DocumentsApplication) context.getApplicationContext(); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 51 | return app.mThumbnailCache; |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 54 | 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, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 65 | public static DocumentClipper getDocumentClipper(Context context) { |
| 66 | return ((DocumentsApplication) context.getApplicationContext()).mClipper; |
| 67 | } |
| 68 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 69 | public static ClipStore getClipStore(Context context) { |
| 70 | return ((DocumentsApplication) context.getApplicationContext()).mClipStore; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 73 | public static DragAndDropManager getDragAndDropManager(Context context) { |
| 74 | return ((DocumentsApplication) context.getApplicationContext()).mDragAndDropManager; |
| 75 | } |
| 76 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public void onCreate() { |
Ben Kwa | f4b0ff6 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 79 | super.onCreate(); |
| 80 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 81 | final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); |
| 82 | final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024; |
| 83 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 84 | mProviders = new ProvidersCache(this); |
| 85 | mProviders.updateAsync(false); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 86 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 87 | mThumbnailCache = new ThumbnailCache(memoryClassBytes / 4); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 88 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 89 | mClipStore = new ClipStorage( |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 90 | ClipStorage.prepareStorage(getCacheDir()), |
| 91 | getSharedPreferences(ClipStorage.PREF_NAME, 0)); |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 92 | mClipper = DocumentClipper.create(this, mClipStore); |
Garfield, Tan | 4d00914 | 2016-05-12 14:12:18 -0700 | [diff] [blame] | 93 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 94 | mDragAndDropManager = DragAndDropManager.create(this, mClipper); |
| 95 | |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 96 | 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 Sharkey | 5916877 | 2013-10-23 09:59:06 -0700 | [diff] [blame] | 100 | packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 101 | packageFilter.addDataScheme("package"); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 102 | registerReceiver(mCacheReceiver, packageFilter); |
| 103 | |
| 104 | final IntentFilter localeFilter = new IntentFilter(); |
| 105 | localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 106 | registerReceiver(mCacheReceiver, localeFilter); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onTrimMemory(int level) { |
| 111 | super.onTrimMemory(level); |
| 112 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 113 | mThumbnailCache.onTrimMemory(level); |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 116 | private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 119 | final Uri data = intent.getData(); |
| 120 | if (data != null) { |
| 121 | final String packageName = data.getSchemeSpecificPart(); |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 122 | mProviders.updatePackageAsync(packageName); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 123 | } else { |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 124 | mProviders.updateAsync(true); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 125 | } |
Jeff Sharkey | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame] | 126 | } |
| 127 | }; |
| 128 | } |