Create ForegroundService
This commit is contained in:
		@ -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<Progress> 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));
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal file
									
								
							@ -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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user