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