-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacotr(Notification): refashioning NotificationHelper, let handle n…
…otifications with FileDownloader more make sense #25
- Loading branch information
Showing
4 changed files
with
435 additions
and
202 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
library/src/main/java/com/liulishuo/filedownloader/notification/BaseNotificationItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright (c) 2015 LingoChamp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.liulishuo.filedownloader.notification; | ||
|
||
import android.app.NotificationManager; | ||
import android.content.Context; | ||
|
||
import com.liulishuo.filedownloader.model.FileDownloadStatus; | ||
import com.liulishuo.filedownloader.util.FileDownloadHelper; | ||
|
||
/** | ||
* @see FileDownloadNotificationHelper | ||
* @see FileDownloadNotificationListener | ||
*/ | ||
public abstract class BaseNotificationItem { | ||
|
||
private int id, sofar, total; | ||
private String title, desc; | ||
|
||
private int status = FileDownloadStatus.INVALID_STATUS; | ||
private int lastStatus = FileDownloadStatus.INVALID_STATUS; | ||
|
||
public BaseNotificationItem(final int id, final String title, final String desc) { | ||
this.id = id; | ||
|
||
this.title = title; | ||
this.desc = desc; | ||
} | ||
|
||
public void show(boolean isShowProgress) { | ||
show(isChanged(), getStatus(), isShowProgress); | ||
} | ||
|
||
/** | ||
* @param isShowProgress Whether there is a need to show the progress schedule changes | ||
*/ | ||
public abstract void show(boolean statusChanged, int status, boolean isShowProgress); | ||
|
||
public void update(final int sofar, final int total) { | ||
this.sofar = sofar; | ||
this.total = total; | ||
show(true); | ||
} | ||
|
||
public void updateStatus(final int status) { | ||
this.status = status; | ||
} | ||
|
||
public void cancel() { | ||
getManager().cancel(id); | ||
} | ||
|
||
private NotificationManager manager; | ||
|
||
protected NotificationManager getManager() { | ||
if (manager == null) { | ||
manager = (NotificationManager) FileDownloadHelper.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE); | ||
} | ||
return manager; | ||
} | ||
|
||
public int getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public int getSofar() { | ||
return sofar; | ||
} | ||
|
||
public void setSofar(int sofar) { | ||
this.sofar = sofar; | ||
} | ||
|
||
public int getTotal() { | ||
return total; | ||
} | ||
|
||
public void setTotal(int total) { | ||
this.total = total; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDesc() { | ||
return desc; | ||
} | ||
|
||
public void setDesc(String desc) { | ||
this.desc = desc; | ||
} | ||
|
||
public int getStatus() { | ||
this.lastStatus = status; | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public int getLastStatus() { | ||
return lastStatus; | ||
} | ||
|
||
public boolean isChanged() { | ||
return this.lastStatus != status; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...c/main/java/com/liulishuo/filedownloader/notification/FileDownloadNotificationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (c) 2015 LingoChamp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.liulishuo.filedownloader.notification; | ||
|
||
import android.util.SparseArray; | ||
|
||
import com.liulishuo.filedownloader.model.FileDownloadStatus; | ||
|
||
/** | ||
* Created by Jacksgong on 9/28/15. | ||
* | ||
* @see BaseNotificationItem | ||
* @see FileDownloadNotificationListener | ||
*/ | ||
public class FileDownloadNotificationHelper<T extends BaseNotificationItem> { | ||
|
||
private final SparseArray<T> notificationArray = new SparseArray<>(); | ||
|
||
/** | ||
* get {@link BaseNotificationItem} by the download id | ||
* | ||
* @param id download id | ||
*/ | ||
public T get(final int id) { | ||
return notificationArray.get(id); | ||
} | ||
|
||
public boolean contains(final int id) { | ||
return get(id) != null; | ||
} | ||
|
||
/** | ||
* remove the {@link BaseNotificationItem} by the download id | ||
* | ||
* @param id download id | ||
* @return removed {@link BaseNotificationItem} | ||
*/ | ||
public T remove(final int id) { | ||
final T n = get(id); | ||
if (n != null) { | ||
notificationArray.remove(id); | ||
return n; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* input a {@link BaseNotificationItem} | ||
*/ | ||
public void add(T notification) { | ||
notificationArray.remove(notification.getId()); | ||
notificationArray.put(notification.getId(), notification); | ||
} | ||
|
||
/** | ||
* show the notification with the exact progress | ||
* | ||
* @param id download id | ||
* @param sofar Number of bytes download so far | ||
* @param total Total bytes | ||
*/ | ||
public void showProgress(final int id, final int sofar, final int total) { | ||
final T notification = get(id); | ||
|
||
if (notification == null) { | ||
return; | ||
} | ||
|
||
notification.updateStatus(FileDownloadStatus.progress); | ||
notification.update(sofar, total); | ||
} | ||
|
||
/** | ||
* show the notification with indeterminate progress | ||
* <p/> | ||
* recommend invoke by pending/retry | ||
* | ||
* @param id download id | ||
* @param status {@link FileDownloadStatus} | ||
*/ | ||
public void showIndeterminate(final int id, int status) { | ||
final BaseNotificationItem notification = get(id); | ||
|
||
if (notification == null) { | ||
return; | ||
} | ||
|
||
notification.updateStatus(status); | ||
notification.show(false); | ||
} | ||
|
||
/** | ||
* cancel the notification by notification id | ||
* <p/> | ||
* recommend invoke by warn/error/completed/(paused) | ||
* | ||
* @param id download id | ||
*/ | ||
public void cancel(final int id) { | ||
final BaseNotificationItem notification = remove(id); | ||
|
||
if (notification == null) { | ||
return; | ||
} | ||
|
||
notification.cancel(); | ||
} | ||
|
||
/** | ||
* clear and cancel all notifications which inside this helper {@link #notificationArray} | ||
*/ | ||
public void clear() { | ||
SparseArray<BaseNotificationItem> cloneArray = | ||
(SparseArray<BaseNotificationItem>) notificationArray.clone(); | ||
notificationArray.clear(); | ||
|
||
for (int i = 0; i < cloneArray.size(); i++) { | ||
final BaseNotificationItem n = cloneArray.get(cloneArray.keyAt(i)); | ||
n.cancel(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.