Files
i2p.android.browser/I2PTunnelForegroundService.java

203 lines
6.8 KiB
Java
Raw Permalink Normal View History

2022-01-06 23:54:55 -05:00
package com.stoutner.privacybrowser.activities;
2022-04-21 20:47:04 -04:00
import android.app.Activity;
2022-01-06 23:54:55 -05:00
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
2022-04-21 20:47:04 -04:00
import android.content.Context;
2022-01-06 23:54:55 -05:00
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;
2022-04-21 20:47:04 -04:00
import com.stoutner.privacybrowser.R;
2022-01-06 23:54:55 -05:00
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import net.i2p.i2ptunnel.TunnelControllerGroup;
import net.i2p.router.Job;
import net.i2p.router.JobImpl;
import net.i2p.router.RouterContext;
2022-04-21 20:47:04 -04:00
public class I2PTunnelForegroundService extends Service {
2022-01-06 23:54:55 -05:00
public static final String CHANNEL_ID = "SAMSAMForegroundServiceChannel";
2022-04-21 20:47:04 -04:00
private final Context mCtx;
private final RouterContext rCtx;
2022-04-21 20:47:04 -04:00
public I2PTunnelForegroundService(Context ctx, RouterContext rctx){
2022-04-21 20:47:04 -04:00
mCtx = ctx;
rCtx = rctx;
2022-04-21 20:47:04 -04:00
//try {
2022-01-06 23:54:55 -05:00
2022-04-21 20:47:04 -04:00
//} catch (IOException e) {
//Log.e("Foreground", e.toString());
//e.printStackTrace();
//}
2022-01-06 23:54:55 -05:00
}
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;
}
2022-04-21 20:47:04 -04:00
public Properties I2PTUNNEL_PROPERTIES() throws IOException {
2022-01-06 23:54:55 -05:00
Log.i("Foreground", "Getting the default properties");
2022-04-21 20:47:04 -04:00
Properties i2ptunnel_properties = new Properties();
return i2ptunnel_properties;
2022-01-06 23:54:55 -05:00
}
@Override
public void onCreate()
{
super.onCreate();
Log.v("SAMForegroundService","onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
2022-04-21 20:47:04 -04:00
PendingIntent newActivity = PendingIntent.getActivity(this, 0, new Intent(this, I2PTunnelService.class), 0);
2022-01-06 23:54:55 -05:00
2022-04-21 20:47:04 -04:00
Intent serviceIntent = new Intent(this, I2PTunnelForegroundService.class);
2022-01-06 23:54:55 -05:00
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();
2022-04-21 20:47:04 -04:00
// Activity MainActivity = (Activity) mCtx;
2022-01-06 23:54:55 -05:00
2022-04-21 20:47:04 -04:00
Intent notificationIntent = new Intent(this, Activity.class);
2022-01-06 23:54:55 -05:00
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())
2022-04-21 20:47:04 -04:00
.addAction(R.drawable.world, "Stop", pStopSelf)
.addAction(R.drawable.world, "NewActivity", newActivity)
2022-01-06 23:54:55 -05:00
.setDefaults(Notification.DEFAULT_ALL)
.build();
if (intent.getAction().equals( "STARTFOREGROUND_ACTION")) {
Log.i("SAMForegroundService", "Received Start Foreground Intent ");
2022-04-21 20:47:04 -04:00
//try {
//SAM_BRIDGE.startup();
//}catch(IOException ioe){
//Log.e("ForegroundService", ioe.toString());
//}
2022-01-06 23:54:55 -05:00
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[]{""};
2022-04-21 20:47:04 -04:00
//SAM_BRIDGE.shutdown(args);
2022-01-06 23:54:55 -05:00
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);
}
}
private RouterContext getContext() {
return rCtx;
}
private class RunI2PTunnel extends JobImpl {
public RunI2PTunnel(RouterContext ctx) {
super(ctx);
}
public String getName() {
return "Start I2P Tunnel";
}
public void runJob() {
if (!getContext().router().isRunning()) {
if (getContext().router().isAlive()) {
//requeue(1000);
} else {
//Util.e("Router stopped before i2ptunnel could start");
}
return;
}
//Util.d("Starting i2ptunnel");
TunnelControllerGroup tcg = TunnelControllerGroup.getInstance(getContext());
try {
tcg.startup();
int sz = tcg.getControllers().size();
//Util.d("i2ptunnel started " + sz + " clients");
// no use starting these until i2ptunnel starts
RouterContext ctx = getContext();
//NewsFetcher fetcher = NewsFetcher.getInstance(mCtx, getContext(), _notif);
//ctx.routerAppManager().addAndStart(fetcher, new String[0]);
//_addressbook = new DaemonThread(new String[] { "addressbook" });
//_addressbook.setName("Addressbook");
//_addressbook.setDaemon(true);
//_addressbook.start();
} catch (IllegalArgumentException iae) {
//Util.e("i2ptunnel failed to start", iae);
}
}
}
2022-04-21 20:47:04 -04:00
}