Skip to content

Commit

Permalink
test(Notification): add update notification with FileDownloader demo
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
Jacksgong committed Feb 2, 2016
1 parent 628e79e commit 3f811a4
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 0 deletions.
4 changes: 4 additions & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<activity
android:name=".TasksManagerDemoActivity"
android:label="@string/tasks_manager_demo_title" />
<activity
android:name=".NotificationDemoActivity"
android:label="@string/notification_demo_title" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void onClickPerformance(final View view) {
startActivity(new Intent(this, PerformanceTestActivity.class));
}

public void onClickNotification(final View view){
startActivity(new Intent(this, NotificationDemoActivity.class));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* 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.demo;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.notification.FileDownloadNotificationListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.liulishuo.filedownloader.notification.BaseNotificationItem;
import com.liulishuo.filedownloader.notification.FileDownloadNotificationHelper;
import com.liulishuo.filedownloader.util.FileDownloadHelper;
import com.liulishuo.filedownloader.util.FileDownloadUtils;

import java.io.File;

/**
* Created by Jacksgong on 2/2/16.
*/
public class NotificationDemoActivity extends AppCompatActivity {
private FileDownloadNotificationHelper<NotificationItem> notificationHelper;

String savePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "notification";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_demo);
notificationHelper = new FileDownloadNotificationHelper<>();

assignViews();

showNotificationCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
clear();
}
}
});

}

private int downloadId = 0;

public void onClickStart(final View view) {
downloadId = FileDownloader.getImpl().create(Constant.BIG_FILE_URLS[2])
.setPath(savePath)
.setListener(new FileDownloadNotificationListener(notificationHelper) {
@Override
protected BaseNotificationItem create(
BaseDownloadTask task) {
return new NotificationItem(task.getDownloadId(), "demo title", "demo desc");
}

@Override
public void createNotification(BaseDownloadTask task) {
super.createNotification(task);
showNotificationCb.setEnabled(false);
}

@Override
public void destroyNotification(BaseDownloadTask task) {
super.destroyNotification(task);
showNotificationCb.setEnabled(true);
}

@Override
protected boolean interceptCancel(BaseDownloadTask task,
BaseNotificationItem n) {
// in this demo, I don't want to cancel the notification, just show for the test
// so return true
return true;
}

@Override
protected boolean disableNotification(BaseDownloadTask task) {
return !showNotificationCb.isChecked();
}

@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
super.pending(task, soFarBytes, totalBytes);
progressBar.setIndeterminate(true);
}

@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
super.progress(task, soFarBytes, totalBytes);
progressBar.setIndeterminate(false);
progressBar.setMax(totalBytes);
progressBar.setProgress(soFarBytes);
}

@Override
protected void completed(BaseDownloadTask task) {
super.completed(task);
progressBar.setIndeterminate(false);
progressBar.setProgress(task.getSmallFileTotalBytes());
}
})
.start();
}

public void onClickPause(final View view) {
FileDownloader.getImpl().pause(downloadId);
}

public void onClickDelete(final View view) {
final File file = new File(savePath);
if (file.exists()) {
file.delete();
}
}

public static class NotificationItem extends BaseNotificationItem {

private NotificationItem(int id, String title, String desc) {
super(id, title, desc);
}

@Override
public void show(boolean statusChanged, int status, boolean isShowProgress) {
NotificationCompat.Builder builder = new NotificationCompat.
Builder(FileDownloadHelper.getAppContext());

String desc = getDesc();
switch (status) {
case FileDownloadStatus.pending:
desc += " pending";
break;
case FileDownloadStatus.progress:
desc += " progress";
break;
case FileDownloadStatus.retry:
desc += " retry";
break;
case FileDownloadStatus.error:
desc += " error";
break;
case FileDownloadStatus.paused:
desc += " paused";
break;
case FileDownloadStatus.completed:
desc += " completed";
break;
case FileDownloadStatus.warn:
desc += " warn";
break;
}

builder.setDefaults(Notification.DEFAULT_LIGHTS)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setContentTitle(getTitle())
.setContentText(desc)
.setSmallIcon(R.mipmap.ic_launcher);

if (statusChanged) {
builder.setTicker(desc);
}

builder.setProgress(getTotal(), getSofar(), !isShowProgress);
getManager().notify(getId(), builder.build());
}
}

@Override
protected void onDestroy() {
this.notificationHelper.clear();
clear();
super.onDestroy();
}

private void clear() {
if (downloadId == 0) {
return;
}
/**
* why not use {@link FileDownloadNotificationHelper#clear()} directly?
* @see FileDownloadNotificationListener#interceptCancel(BaseDownloadTask, BaseNotificationItem)
*/
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).
cancel(downloadId);
}

private CheckBox showNotificationCb;
private ProgressBar progressBar;

private void assignViews() {
showNotificationCb = (CheckBox) findViewById(R.id.show_notification_cb);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}

}
5 changes: 5 additions & 0 deletions demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
android:onClick="onClickPerformance"
android:text="@string/performance_test_title" />

<Button
style="@style/DemoButton"
android:onClick="onClickNotification"
android:text="@string/notification_demo_title" />


</LinearLayout>
76 changes: 76 additions & 0 deletions demo/src/main/res/layout/activity_notification_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:onClick="onClickStart"
android:text="@string/start"
android:textColor="#000000"
android:textSize="14sp" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:onClick="onClickPause"
android:text="@string/pause"
android:textColor="#000000"
android:textSize="14sp" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:onClick="onClickDelete"
android:text="@string/delete"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>


<CheckBox
android:id="@+id/show_notification_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/notification_demo_show"
android:textColor="@android:color/black"
android:textSize="16sp" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp" />

</LinearLayout>
4 changes: 4 additions & 0 deletions demo/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@
<string name="hybrid_test_multiple_tasks_serial_title">多任务队列串行下载</string>
<string name="hybrid_test_single_task_title">单任务下载测试</string>
<string name="avoid_missing_screen_frames">避免掉帧</string>


<string name="notification_demo_title">下载通知案例</string>
<string name="notification_demo_show">显示通知</string>
</resources>
2 changes: 2 additions & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
<string name="hybrid_test_single_task_title">Execute single task</string>
<string name="avoid_missing_screen_frames">Avoid Missing Screen Frames</string>

<string name="notification_demo_title">Notification Demo</string>
<string name="notification_demo_show">Show the Notification</string>
</resources>

0 comments on commit 3f811a4

Please sign in to comment.