OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.download.items; | |
6 | |
7 import android.content.ComponentName; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.content.ServiceConnection; | |
11 import android.os.IBinder; | |
12 | |
13 import org.chromium.base.Log; | |
14 import org.chromium.chrome.browser.download.DownloadNotificationService; | |
15 import org.chromium.chrome.browser.download.items.OfflineContentAggregatorNotifi er.NotifierUi; | |
16 import org.chromium.components.offline_items_collection.ContentId; | |
17 import org.chromium.components.offline_items_collection.OfflineItem; | |
18 | |
19 import javax.annotation.Nullable; | |
20 | |
21 /** | |
22 * A implementation of {@link NotifierUi} that connects to the {@link DownloadNo tificationService}. | |
23 * @see OfflineContentAggregatorNotifier | |
24 */ | |
25 public class DownloadNotificationServiceNotifierUi | |
26 implements NotifierUi, ServiceConnection, DownloadNotificationService.Ob server { | |
27 private static final String TAG = "DownloadNotifierUi"; | |
28 | |
29 private final Context mApplicationContext; | |
30 | |
31 /** Whether or not the underlying service is bound or awaiting a bind reques t result. */ | |
32 private boolean mIsServiceBound; | |
33 | |
34 /** Could be {@code null} if the service is unbound or hasn't been started. */ | |
35 @Nullable | |
36 private DownloadNotificationService mBoundService; | |
37 | |
38 /** The {@link Runnable} that should be called when our service is bound. */ | |
39 @Nullable | |
40 private Runnable mOnBoundRunnable; | |
41 | |
42 /** | |
43 * Creates an instance of the {@link DownloadNotificationServiceNotifierUi}. Does not start | |
44 * or bind the {@link android.app.Service}. | |
45 * @param context The {@link Context} to use to do all {@link android.app.Se rvice} interactions. | |
46 */ | |
47 public DownloadNotificationServiceNotifierUi(Context context) { | |
48 mApplicationContext = context.getApplicationContext(); | |
gone
2017/03/20 19:03:36
Do you have a good reason not to use ContextUtils.
David Trainor- moved to gerrit
2017/03/25 03:31:13
Eh just a bit cleaner to pass in dependencies. I'
| |
49 } | |
50 | |
51 private void startAndBindService() { | |
52 DownloadNotificationService.startDownloadNotificationService(mApplicatio nContext, null); | |
53 mApplicationContext.bindService( | |
54 new Intent(mApplicationContext, DownloadNotificationService.clas s), this, | |
55 Context.BIND_AUTO_CREATE); | |
56 } | |
57 | |
58 private void unbindService() { | |
59 mApplicationContext.unbindService(this); | |
60 } | |
61 | |
62 // OfflineContentAggregatorNotifier.NotifierUi implementation. | |
63 @Override | |
64 public boolean onUiNeeded(Runnable onReadyEvent) { | |
65 if (!mIsServiceBound) startAndBindService(); | |
66 mIsServiceBound = true; | |
gone
2017/03/20 19:03:35
seems like this boolean should go where you're act
David Trainor- moved to gerrit
2017/03/25 03:31:12
I didn't do it originally because I wanted to make
| |
67 | |
68 boolean isReady = mBoundService != null; | |
69 mOnBoundRunnable = isReady ? onReadyEvent : null; | |
70 | |
71 return isReady; | |
72 } | |
73 | |
74 @Override | |
75 public void onUiNotNeeded() { | |
gone
2017/03/20 19:03:36
What if onUiNeeded was called and the mOnBoundRunn
| |
76 if (!mIsServiceBound) return; | |
77 if (mBoundService != null) mBoundService.removeObserver(this); | |
78 unbindService(); | |
79 mBoundService = null; | |
fgorski
2017/03/20 20:19:20
why not reset the values in unbindService?
David Trainor- moved to gerrit
2017/03/25 03:31:13
See above comment on testing. Will change though
| |
80 mIsServiceBound = false; | |
81 } | |
82 | |
83 @Override | |
84 public void updateItem(OfflineItem item) { | |
85 // TODO(dtrainor): Interact with the DownloadNotificationService. | |
David Trainor- moved to gerrit
2017/03/18 00:45:41
This and "removeItem" will be implemented in a fut
| |
86 } | |
87 | |
88 @Override | |
89 public void removeItem(ContentId id) { | |
90 // TODO(dtrainor): Interact with the DownloadNotificationService. | |
91 } | |
92 | |
93 // ServiceConnection implementation. | |
94 @Override | |
95 public void onServiceConnected(ComponentName className, IBinder service) { | |
96 if (!(service instanceof DownloadNotificationService.LocalBinder)) { | |
97 Log.w(TAG, | |
98 "Not from DownloadNotificationService, do not connect." | |
99 + " Component name: " + className); | |
100 assert false; | |
101 return; | |
102 } | |
103 mBoundService = ((DownloadNotificationService.LocalBinder) service).getS ervice(); | |
104 mBoundService.addObserver(this); | |
105 | |
106 if (mOnBoundRunnable != null) { | |
107 mOnBoundRunnable.run(); | |
108 mOnBoundRunnable = null; | |
109 } | |
110 } | |
111 | |
112 @Override | |
113 public void onServiceDisconnected(ComponentName className) {} | |
114 | |
115 // DownloadNotificationService.Observer implementation. | |
116 @Override | |
117 public void onServiceShutdownRequested() { | |
118 onUiNotNeeded(); | |
119 } | |
120 | |
121 @Override | |
122 public void onDownloadCanceled(String guid) { | |
123 // We rely on the cancel event to forward an update to the UI. | |
124 } | |
125 } | |
OLD | NEW |