From 493e836f5673c291aff575edc6fb56d8666169d3 Mon Sep 17 00:00:00 2001 From: lionel Date: Fri, 12 Mar 2021 10:32:47 +0100 Subject: [PATCH] Create ForegroundService --- app/src/main/AndroidManifest.xml | 17 +-- .../main/java/com/localtransfer/Progress.java | 102 ++++++++++-------- .../com/localtransfer/TransferService.java | 43 ++++++++ .../fragment/ProgressFragment.java | 2 +- 4 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/com/localtransfer/TransferService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ae3108f..bd2ee7c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,10 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> + - - + android:label="@string/title_activity_settings"> - + - + + - + + - diff --git a/app/src/main/java/com/localtransfer/Progress.java b/app/src/main/java/com/localtransfer/Progress.java index 983ffe5..19dc597 100644 --- a/app/src/main/java/com/localtransfer/Progress.java +++ b/app/src/main/java/com/localtransfer/Progress.java @@ -1,5 +1,6 @@ package com.localtransfer; +import android.app.Notification; import android.app.PendingIntent; import android.content.Intent; import android.view.LayoutInflater; @@ -12,6 +13,7 @@ import android.widget.TextView; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; +import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.ConcurrentModificationException; @@ -25,17 +27,24 @@ public class Progress { public static final int DOWNLOAD = 1001; - private static NotificationCompat.Builder bl = new NotificationCompat.Builder(Transfer.activity, null); + private static Notification.Builder bl = new Notification.Builder(Transfer.activity); private static NotificationManagerCompat notifiManager = NotificationManagerCompat.from(Transfer.activity); private static final LayoutInflater inflater = (LayoutInflater) Transfer.activity.getSystemService(Transfer.activity.LAYOUT_INFLATER_SERVICE); + private static Intent notificationIntent = new Intent(Transfer.activity, MainActivity.class); + private static PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity,0, notificationIntent, 0); + private static List instances = new ArrayList<>(); private Integer id; private String name; + private long size; + private String HumanSize; public long loaded; + private String HumanLoaded; + public long percent; public static View root; @@ -47,7 +56,7 @@ public class Progress { private String info; public Button button; - private boolean run = false; + private boolean run; public Progress(String name, long size, int type) { @@ -57,6 +66,7 @@ public class Progress { this.name = name; this.size = size; + this.HumanSize = Transfer.humanReadableByteCountBin(size); id = View.generateViewId(); @@ -73,6 +83,10 @@ public class Progress { throw new IllegalStateException("Unexpected value: " + type); } + Intent serviceIntent = new Intent(Transfer.activity, TransferService.class); + serviceIntent.putExtra("id", id); + ContextCompat.startForegroundService(Transfer.activity, serviceIntent); + timer.schedule(new TimerTask() { @Override public void run() { @@ -91,15 +105,18 @@ public class Progress { run = false; + Intent serviceIntent = new Intent(Transfer.activity, TransferService.class); + Transfer.activity.stopService(serviceIntent); + if(! app_started) { bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud) .setContentTitle(name) .setContentText(info) .setProgress(0, 0, false) - .setAutoCancel(true) - .setOngoing(false); + .setContentIntent(pendingIntent); notifiManager.notify(id, bl.build()); } + } public void delete() { @@ -127,55 +144,21 @@ public class Progress { return false; } - - public void showProgress(){ + private void showProgress(){ try { - String HumanLoaded = Transfer.humanReadableByteCountBin(loaded); - String HumanSize = Transfer.humanReadableByteCountBin(size); + HumanLoaded = Transfer.humanReadableByteCountBin(loaded); - if(app_started) { - notifiManager.cancel(id); - } - else { - - Intent intent = new Intent(Transfer.activity, MainActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity, 0, intent, 0); - - bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud) - .setContentTitle(name) - .setContentText(String.format("%d%% %s/%s", percent, HumanLoaded, HumanSize)) - .setProgress(100, (int) percent, false) - .setContentIntent(pendingIntent) - .setAutoCancel(true) - .setOngoing(true); - notifiManager.notify(id, bl.build()); - } + bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud) + .setContentTitle(name) + .setContentText(String.format("%d%% %s/%s", percent, HumanLoaded, HumanSize)) + .setProgress(100, (int) percent, false) + .setContentIntent(pendingIntent); + notifiManager.notify(id, bl.build()); if(app_started && fragment_on) { - LinearLayout groot = root.findViewById(R.id.groot); - - final View progress; - - if (groot.findViewById(id) != null) { - progress = groot.findViewById(id); - } else { - progress = inflater.inflate(R.layout.progress, null); - progress.setId(id); - Transfer.activity.runOnUiThread(() -> { - groot.addView(progress, 0); - }); - } - - Transfer.activity.runOnUiThread(() -> { - ((ProgressBar) progress.findViewById(R.id.progressBar)).setProgress((int) percent); - ((ImageView) progress.findViewById(R.id.transferType)).setImageResource(drawable); - ((TextView) progress.findViewById(R.id.progressText)).setText(percent + " %"); - ((TextView) progress.findViewById(R.id.fileName)).setText(name); - ((TextView) progress.findViewById(R.id.fileSize)).setText(String.format("%s/%s", HumanLoaded, HumanSize)); - }); + showProgressFragment(); } @@ -185,4 +168,29 @@ public class Progress { } + public void showProgressFragment(){ + + LinearLayout groot = root.findViewById(R.id.groot); + + final View progress; + + if (groot.findViewById(id) != null) { + progress = groot.findViewById(id); + } else { + progress = inflater.inflate(R.layout.progress, null); + progress.setId(id); + Transfer.activity.runOnUiThread(() -> { + groot.addView(progress, 0); + }); + } + + Transfer.activity.runOnUiThread(() -> { + ((ProgressBar) progress.findViewById(R.id.progressBar)).setProgress((int) percent); + ((ImageView) progress.findViewById(R.id.transferType)).setImageResource(drawable); + ((TextView) progress.findViewById(R.id.progressText)).setText(percent + " %"); + ((TextView) progress.findViewById(R.id.fileName)).setText(name); + ((TextView) progress.findViewById(R.id.fileSize)).setText(String.format("%s/%s", HumanLoaded, HumanSize)); + }); + + } } diff --git a/app/src/main/java/com/localtransfer/TransferService.java b/app/src/main/java/com/localtransfer/TransferService.java new file mode 100644 index 0000000..894e9a0 --- /dev/null +++ b/app/src/main/java/com/localtransfer/TransferService.java @@ -0,0 +1,43 @@ +package com.localtransfer; + +import android.app.Notification; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +import androidx.core.app.NotificationCompat; + +public class TransferService extends Service { + + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + int id = intent.getIntExtra("id", 0); + + Intent notificationIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, + 0, notificationIntent, 0); + + Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID") + .setContentIntent(pendingIntent) + .build(); + + startForeground(id, notification); + + //do heavy work on a background thread + //stopSelf(); + + return START_NOT_STICKY; + } + + @Override + public IBinder onBind(Intent intent) { + // TODO: Return the communication channel to the service. + throw new UnsupportedOperationException("Not yet implemented"); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/localtransfer/fragment/ProgressFragment.java b/app/src/main/java/com/localtransfer/fragment/ProgressFragment.java index 57396f0..afe7cc0 100644 --- a/app/src/main/java/com/localtransfer/fragment/ProgressFragment.java +++ b/app/src/main/java/com/localtransfer/fragment/ProgressFragment.java @@ -57,7 +57,7 @@ public class ProgressFragment extends Fragment { List instances = Progress.getInstances(); for (Object obj: instances) { Progress p = (Progress) obj; - p.showProgress(); + p.showProgressFragment(); } }