From 3480ea6c158f1ac5436a31c14c0f48cca4d0fd7e Mon Sep 17 00:00:00 2001 From: Aryan Najafi Date: Thu, 14 Nov 2019 15:50:30 -0600 Subject: [PATCH] Add quick settings tile --- Android/app/src/main/AndroidManifest.xml | 17 +++++ .../java/app/intra/sys/IntraTileService.java | 74 +++++++++++++++++++ .../java/app/intra/sys/IntraVpnService.java | 13 ++++ Android/app/src/main/res/drawable/ic_tile.xml | 12 +++ 4 files changed, 116 insertions(+) create mode 100644 Android/app/src/main/java/app/intra/sys/IntraTileService.java create mode 100644 Android/app/src/main/res/drawable/ic_tile.xml diff --git a/Android/app/src/main/AndroidManifest.xml b/Android/app/src/main/AndroidManifest.xml index d346c531..491a8b78 100644 --- a/Android/app/src/main/AndroidManifest.xml +++ b/Android/app/src/main/AndroidManifest.xml @@ -19,6 +19,10 @@ + + + + + + + + + + + diff --git a/Android/app/src/main/java/app/intra/sys/IntraTileService.java b/Android/app/src/main/java/app/intra/sys/IntraTileService.java new file mode 100644 index 00000000..7e205065 --- /dev/null +++ b/Android/app/src/main/java/app/intra/sys/IntraTileService.java @@ -0,0 +1,74 @@ +/* +Copyright 2019 Jigsaw Operations LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package app.intra.sys; + +import android.content.ComponentName; +import android.content.Intent; +import android.net.VpnService; +import android.os.Build; +import android.os.IBinder; +import android.service.quicksettings.Tile; +import android.service.quicksettings.TileService; + +import androidx.annotation.RequiresApi; + +import app.intra.ui.MainActivity; + +@RequiresApi(api = Build.VERSION_CODES.N) +public class IntraTileService extends TileService { + + @Override + public void onStartListening() { + VpnState vpnState = VpnController.getInstance().getState(this); + + Tile tile = getQsTile(); + + if (vpnState.activationRequested) { + tile.setState(Tile.STATE_ACTIVE); + } else { + tile.setState(Tile.STATE_INACTIVE); + } + + tile.updateTile(); + } + + @Override + public void onClick() { + VpnState vpnState = VpnController.getInstance().getState(this); + + if (vpnState.activationRequested) { + VpnController.getInstance().stop(this); + } else { + if (VpnService.prepare(this) == null) { + // Start VPN service when VPN permission has been granted. + VpnController.getInstance().start(this); + } else { + // Open Main activity when VPN permission has not been granted. + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivityAndCollapse(intent); + } + } + } + + @Override + public IBinder onBind(Intent intent) { + // Update tile state on boot. + TileService.requestListeningState(this, + new ComponentName(this, IntraTileService.class)); + return super.onBind(intent); + } +} diff --git a/Android/app/src/main/java/app/intra/sys/IntraVpnService.java b/Android/app/src/main/java/app/intra/sys/IntraVpnService.java index 608471cf..5667d9c6 100644 --- a/Android/app/src/main/java/app/intra/sys/IntraVpnService.java +++ b/Android/app/src/main/java/app/intra/sys/IntraVpnService.java @@ -19,6 +19,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -32,6 +33,7 @@ import android.os.Build.VERSION_CODES; import android.os.SystemClock; import android.preference.PreferenceManager; +import android.service.quicksettings.TileService; import android.util.Log; import androidx.annotation.WorkerThread; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -176,6 +178,8 @@ public synchronized int onStartCommand(Intent intent, int flags, int startId) { startForeground(SERVICE_ID, builder.getNotification()); + updateQuickSettingsTile(); + return START_REDELIVER_INTENT; } @@ -354,6 +358,8 @@ public void signalStopService(boolean userInitiated) { stopVpnAdapter(); stopSelf(); + + updateQuickSettingsTile(); } private VpnAdapter makeVpnAdapter() { @@ -395,6 +401,13 @@ private synchronized void stopVpnAdapter() { } } + private void updateQuickSettingsTile() { + if (VERSION.SDK_INT >= VERSION_CODES.N) { + TileService.requestListeningState(this, + new ComponentName(this, IntraTileService.class)); + } + } + @Override public synchronized void onDestroy() { LogWrapper.log(Log.INFO, LOG_TAG, "Destroying DNS VPN service"); diff --git a/Android/app/src/main/res/drawable/ic_tile.xml b/Android/app/src/main/res/drawable/ic_tile.xml new file mode 100644 index 00000000..2c0c4329 --- /dev/null +++ b/Android/app/src/main/res/drawable/ic_tile.xml @@ -0,0 +1,12 @@ + + + +