package com.stoutner.privacybrowser.activities; import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import com.stoutner.privacybrowser.R; import java.io.File; import java.io.IOException; import java.util.Properties; public class I2PTunnelForegroundService extends Service { public static final String CHANNEL_ID = "SAMSAMForegroundServiceChannel"; private final Context mCtx; public I2PTunnelForegroundService(Context ctx){ mCtx = ctx; //try { //} catch (IOException e) { //Log.e("Foreground", e.toString()); //e.printStackTrace(); //} } public Uri resToURI(int resourceId) { Resources resources = getResources(); Log.i("Foreground", "Getting the default properties"); Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); return uri; } public Properties I2PTUNNEL_PROPERTIES() throws IOException { Log.i("Foreground", "Getting the default properties"); Properties i2ptunnel_properties = new Properties(); return i2ptunnel_properties; } @Override public void onCreate() { super.onCreate(); Log.v("SAMForegroundService","onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { PendingIntent newActivity = PendingIntent.getActivity(this, 0, new Intent(this, I2PTunnelService.class), 0); Intent serviceIntent = new Intent(this, I2PTunnelForegroundService.class); serviceIntent.putExtra("inputExtra", "Stop Foreground Service in Android"); serviceIntent.setAction("STOPFOREGROUND_ACTION"); PendingIntent pStopSelf = PendingIntent.getService(this, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT); String input = intent.getStringExtra("inputExtra"); Log.v("SAMSAMForegroundService" , input); createNotificationChannel(); // Activity MainActivity = (Activity) mCtx; Intent notificationIntent = new Intent(this, Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("SAM API Service") .setContentText("SAM API Service is running") .setSmallIcon(android.R.drawable.ic_dialog_alert) .setContentIntent(pendingIntent) .setLargeIcon(BitmapFactory.decodeResource(getResources(),android.R.drawable.ic_dialog_alert)) .setWhen(System.currentTimeMillis()) .addAction(R.drawable.world, "Stop", pStopSelf) .addAction(R.drawable.world, "NewActivity", newActivity) .setDefaults(Notification.DEFAULT_ALL) .build(); if (intent.getAction().equals( "STARTFOREGROUND_ACTION")) { Log.i("SAMForegroundService", "Received Start Foreground Intent "); //try { //SAM_BRIDGE.startup(); //}catch(IOException ioe){ //Log.e("ForegroundService", ioe.toString()); //} startForeground(1, notification); } else if (intent.getAction().equals( "STOPFOREGROUND_ACTION")) { Log.i("SAMForegroundService", "Received Stop Foreground Intent"); //your end servce code String args[] = new String[]{""}; //SAM_BRIDGE.shutdown(args); stopForeground(true); stopSelfResult(startId); } return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel); } } }