added cancel button on download notification

and fixed servive bugs when app was killed and reopened
pull/1/head
Denis Çerri 4 years ago
parent 8910d19815
commit 0ca90d17a3
No known key found for this signature in database
GPG Key ID: 3F50F14A8E7F7A13

@ -49,6 +49,7 @@
android:name=".DownloaderService" android:name=".DownloaderService"
android:enabled="true" android:enabled="true"
android:exported="false" /> android:exported="false" />
<receiver android:name=".receiver.NotificationReceiver" />
</application> </application>
</manifest> </manifest>

@ -124,10 +124,10 @@ public class DownloaderService extends Service {
@Override @Override
public boolean onUnbind(Intent intent) { public void onDestroy() {
super.onDestroy();
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
return super.onUnbind(intent);
} }
public class LocalBinder extends Binder implements IDownloaderService { public class LocalBinder extends Binder implements IDownloaderService {
@ -171,7 +171,9 @@ public class DownloaderService extends Service {
YoutubeDL.getInstance().destroyProcessById(downloadProcessID); YoutubeDL.getInstance().destroyProcessById(downloadProcessID);
compositeDisposable.clear(); compositeDisposable.clear();
//stopForeground(true); //stopForeground(true);
if (cancelAll) onDownloadCancelAll(); if (cancelAll) {
onDownloadCancelAll();
}
}catch(Exception err){ }catch(Exception err){
Log.e(TAG, err.getMessage()); Log.e(TAG, err.getMessage());
} }

@ -131,6 +131,17 @@ public class MainActivity extends AppCompatActivity{
handleIntents(intent); handleIntents(intent);
} }
@Override
protected void onDestroy() {
super.onDestroy();
try {
iDownloaderService.removeActivity(this);
context.getApplicationContext().unbindService(serviceConnection);
}catch (Exception e){
e.printStackTrace();
}
}
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
super.onNewIntent(intent); super.onNewIntent(intent);
@ -181,6 +192,7 @@ public class MainActivity extends AppCompatActivity{
if(!listeners.contains(awaitingListener)) listeners.add(awaitingListener); if(!listeners.contains(awaitingListener)) listeners.add(awaitingListener);
Intent serviceIntent = new Intent(context, DownloaderService.class); Intent serviceIntent = new Intent(context, DownloaderService.class);
serviceIntent.putParcelableArrayListExtra("queue", downloadQueue); serviceIntent.putParcelableArrayListExtra("queue", downloadQueue);
context.getApplicationContext().startService(serviceIntent);
context.getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); context.getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} }
@ -195,6 +207,7 @@ public class MainActivity extends AppCompatActivity{
dbManager.addToHistory(v); dbManager.addToHistory(v);
} }
dbManager.close(); dbManager.close();
downloadsFragment.setDownloading(true);
downloadsFragment.initCards(); downloadsFragment.initCards();
} }
} catch (Exception e) { } catch (Exception e) {
@ -204,12 +217,11 @@ public class MainActivity extends AppCompatActivity{
public void stopDownloadService(){ public void stopDownloadService(){
if(!isDownloadServiceRunning) return; if(!isDownloadServiceRunning) return;
iDownloaderService.removeActivity(this); try {
context.getApplicationContext().unbindService(serviceConnection); iDownloaderService.removeActivity(this);
downloaderService.stopForeground(true); context.getApplicationContext().unbindService(serviceConnection);
downloaderService.stopSelf(); context.getApplicationContext().stopService(new Intent(context.getApplicationContext(), DownloaderService.class));
NotificationUtil notificationUtil = new NotificationUtil(context.getApplicationContext()); }catch (Exception ignored){}
notificationUtil.cancelDownloadNotification(NotificationUtil.DOWNLOAD_NOTIFICATION_ID);
isDownloadServiceRunning = false; isDownloadServiceRunning = false;
} }
@ -226,8 +238,10 @@ public class MainActivity extends AppCompatActivity{
public boolean isDownloadServiceRunning() { public boolean isDownloadServiceRunning() {
ActivityManager.RunningServiceInfo service = getService(DownloaderService.class); ActivityManager.RunningServiceInfo service = getService(DownloaderService.class);
if(service != null){ if(service != null){
isDownloadServiceRunning = true; if (service.foreground) {
return true; isDownloadServiceRunning = true;
return true;
}
} }
return false; return false;
} }

@ -56,7 +56,7 @@ import java.util.Locale;
* A fragment representing a list of Items. * A fragment representing a list of Items.
*/ */
public class DownloadsFragment extends Fragment implements DownloadsRecyclerViewAdapter.OnItemClickListener, View.OnClickListener{ public class DownloadsFragment extends Fragment implements DownloadsRecyclerViewAdapter.OnItemClickListener, View.OnClickListener{
private boolean downloading = false;
private View fragmentView; private View fragmentView;
private DBManager dbManager; private DBManager dbManager;
Context context; Context context;
@ -93,6 +93,7 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
progressBar = fragmentView.findViewWithTag(v.getURL()+v.getDownloadedType()+"##progress"); progressBar = fragmentView.findViewWithTag(v.getURL()+v.getDownloadedType()+"##progress");
} }
}catch(Exception ignored){} }catch(Exception ignored){}
downloading = true;
} }
public void onDownloadProgress(DownloadInfo info) { public void onDownloadProgress(DownloadInfo info) {
@ -116,6 +117,7 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
dbManager = new DBManager(context); dbManager = new DBManager(context);
dbManager.clearHistoryItem(v, false); dbManager.clearHistoryItem(v, false);
downloadsRecyclerViewAdapter.notifyItemRemoved(position); downloadsRecyclerViewAdapter.notifyItemRemoved(position);
downloading = false;
}catch(Exception ignored){} }catch(Exception ignored){}
} }
@ -171,6 +173,7 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
dbManager.close(); dbManager.close();
downloadsRecyclerViewAdapter.notifyItemChanged(downloadsObjects.indexOf(item)); downloadsRecyclerViewAdapter.notifyItemChanged(downloadsObjects.indexOf(item));
} catch (Exception ignored) {} } catch (Exception ignored) {}
downloading = false;
}catch(Exception ignored){} }catch(Exception ignored){}
} }
@ -182,16 +185,41 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
downloadsObjects.remove(v); downloadsObjects.remove(v);
dbManager = new DBManager(context); dbManager = new DBManager(context);
dbManager.clearHistoryItem(v,false); dbManager.clearHistoryItem(v,false);
dbManager.close();
downloadsRecyclerViewAdapter.setVideoList(downloadsObjects); downloadsRecyclerViewAdapter.setVideoList(downloadsObjects);
if (downloadsObjects.isEmpty()) initCards();
downloading = false;
}catch (Exception ignored){} }catch (Exception ignored){}
} }
@Override @Override
public void onDownloadCancelAll(DownloadInfo downloadInfo){} public void onDownloadCancelAll(DownloadInfo downloadInfo){
try {
dbManager = new DBManager(context);
while (downloadsObjects.size() > 0){
Video v = downloadsObjects.get(0);
if (v.isQueuedDownload()){
dbManager.clearHistoryItem(v, false);
downloadsObjects.remove(v);
downloadsRecyclerViewAdapter.setVideoList(downloadsObjects);
}else{
break;
}
}
dbManager.close();
if (downloadsObjects.isEmpty()) initCards();
downloading = false;
}catch (Exception ignored){}
}
public void onDownloadServiceEnd() {} public void onDownloadServiceEnd() {}
}; };
public void setDownloading(boolean d){
downloading = d;
}
public DownloadsFragment() { public DownloadsFragment() {
} }
@ -226,9 +254,8 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
selectionChips = fragmentView.findViewById(R.id.downloads_selection_chips); selectionChips = fragmentView.findViewById(R.id.downloads_selection_chips);
websiteGroup = fragmentView.findViewById(R.id.website_chip_group); websiteGroup = fragmentView.findViewById(R.id.website_chip_group);
uiHandler = new Handler(Looper.getMainLooper()); uiHandler = new Handler(Looper.getMainLooper());
dbManager = new DBManager(context);
downloadsObjects = new ArrayList<>(); downloadsObjects = new ArrayList<>();
downloading = mainActivity.isDownloadServiceRunning();
recyclerView = fragmentView.findViewById(R.id.recycler_view_downloads); recyclerView = fragmentView.findViewById(R.id.recycler_view_downloads);
downloadsRecyclerViewAdapter = new DownloadsRecyclerViewAdapter(downloadsObjects, this, activity); downloadsRecyclerViewAdapter = new DownloadsRecyclerViewAdapter(downloadsObjects, this, activity);
@ -247,8 +274,11 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
downloadsRecyclerViewAdapter.clear(); downloadsRecyclerViewAdapter.clear();
no_results.setVisibility(View.GONE); no_results.setVisibility(View.GONE);
selectionChips.setVisibility(View.VISIBLE); selectionChips.setVisibility(View.VISIBLE);
dbManager = new DBManager(context);
try{ try{
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
if (!downloading) dbManager.clearDownloadingHistory();
downloadsObjects = dbManager.getHistory("", format,website,sort); downloadsObjects = dbManager.getHistory("", format,website,sort);
uiHandler.post(() -> { uiHandler.post(() -> {
downloadsRecyclerViewAdapter.setVideoList(downloadsObjects); downloadsRecyclerViewAdapter.setVideoList(downloadsObjects);
@ -264,6 +294,7 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
websiteGroup.removeAllViews(); websiteGroup.removeAllViews();
}); });
} }
dbManager.close();
}); });
thread.start(); thread.start();
}catch(Exception e){ }catch(Exception e){
@ -297,6 +328,8 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
searchView.setInputType(InputType.TYPE_CLASS_TEXT); searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.setQueryHint(getString(R.string.search_history_hint)); searchView.setQueryHint(getString(R.string.search_history_hint));
dbManager = new DBManager(context);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {

@ -179,7 +179,9 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
downloading = false; downloading = false;
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false); topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false);
}catch(Exception ignored){} }catch(Exception e){
e.printStackTrace();
}
} }
@ -257,9 +259,9 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
homeRecyclerViewAdapter.clear(); homeRecyclerViewAdapter.clear();
shimmerCards.startShimmer(); shimmerCards.startShimmer();
shimmerCards.setVisibility(View.VISIBLE); shimmerCards.setVisibility(View.VISIBLE);
Handler uiHandler = new Handler(Looper.getMainLooper());
try { try {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
Handler uiHandler = new Handler(Looper.getMainLooper());
dbManager = new DBManager(context); dbManager = new DBManager(context);
resultObjects = dbManager.getResults(); resultObjects = dbManager.getResults();
String playlistTitle = ""; String playlistTitle = "";
@ -272,12 +274,6 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
resultObjects = infoUtil.getTrending(context); resultObjects = infoUtil.getTrending(context);
dbManager.addToResults(resultObjects); dbManager.addToResults(resultObjects);
uiHandler.post(() -> {
homeRecyclerViewAdapter.setVideoList(resultObjects, true);
shimmerCards.stopShimmer();
shimmerCards.setVisibility(View.GONE);
});
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@ -291,12 +287,15 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
updateDownloadingStatusOnResult(tmp, "video", false); updateDownloadingStatusOnResult(tmp, "video", false);
} }
} }
uiHandler.post(() -> {
shimmerCards.stopShimmer();
shimmerCards.setVisibility(View.GONE);
});
} }
} }
uiHandler.post(() -> {
homeRecyclerViewAdapter.setVideoList(resultObjects, true);
shimmerCards.stopShimmer();
shimmerCards.setVisibility(View.GONE);
});
dbManager.close(); dbManager.close();
if (resultObjects != null) { if (resultObjects != null) {
uiHandler.post(this::scrollToTop); uiHandler.post(this::scrollToTop);
@ -308,6 +307,10 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
thread.start(); thread.start();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
uiHandler.post(() -> {
shimmerCards.stopShimmer();
shimmerCards.setVisibility(View.GONE);
});
} }
} }
@ -328,7 +331,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
} }
}; };
if(mainActivity.isDownloadServiceRunning()){ if(downloading){
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(true); topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(true);
} }

@ -0,0 +1,59 @@
package com.deniscerri.ytdlnis.receiver;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import com.deniscerri.ytdlnis.DownloaderService;
import com.deniscerri.ytdlnis.MainActivity;
import com.deniscerri.ytdlnis.service.IDownloaderListener;
import com.deniscerri.ytdlnis.service.IDownloaderService;
import com.deniscerri.ytdlnis.util.NotificationUtil;
import java.util.ArrayList;
public class NotificationReceiver extends BroadcastReceiver {
public DownloaderService downloaderService;
private ArrayList<IDownloaderListener> listeners = null;
private IDownloaderService iDownloaderService;
private Context context;
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
downloaderService = ((DownloaderService.LocalBinder) service).getService();
iDownloaderService = (IDownloaderService) service;
cancelDownload();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
downloaderService = null;
iDownloaderService = null;
}
};
@Override
public void onReceive(Context c, Intent intent) {
context = c;
String message = intent.getStringExtra("cancel");
if (message != null){
Intent serviceIntent = new Intent(context.getApplicationContext(), DownloaderService.class);
serviceIntent.putExtra("rebind", true);
context.getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
private void cancelDownload(){
try {
iDownloaderService.cancelDownload(true);
context.getApplicationContext().unbindService(serviceConnection);
context.getApplicationContext().stopService(new Intent(context.getApplicationContext(), DownloaderService.class));
}catch (Exception ignored){}
}
}

@ -5,12 +5,14 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import com.deniscerri.ytdlnis.R; import com.deniscerri.ytdlnis.R;
import com.deniscerri.ytdlnis.receiver.NotificationReceiver;
public class NotificationUtil { public class NotificationUtil {
Context context; Context context;
@ -53,6 +55,16 @@ public class NotificationUtil {
} }
public Notification createDownloadServiceNotification(PendingIntent pendingIntent, String title){ public Notification createDownloadServiceNotification(PendingIntent pendingIntent, String title){
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("cancel", "");
PendingIntent cancelNotificationPendingIntent = PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
);
Notification notification = notificationBuilder Notification notification = notificationBuilder
.setContentTitle(title) .setContentTitle(title)
.setOngoing(true) .setOngoing(true)
@ -65,6 +77,8 @@ public class NotificationUtil {
.setProgress(PROGRESS_MAX, PROGRESS_CURR, false) .setProgress(PROGRESS_MAX, PROGRESS_CURR, false)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE) .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.clearActions()
.addAction(0, context.getString(R.string.cancel), cancelNotificationPendingIntent)
.build(); .build();
return notification; return notification;

Loading…
Cancel
Save