Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quick settings tile. #233

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Open MainActivity on quick settings tile long click -->
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter>
</activity>

<service
Expand All @@ -31,6 +35,19 @@
</intent-filter>
</service>

<service
android:name="app.intra.sys.IntraTileService"
android:icon="@drawable/ic_tile"
android:label="@string/app_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter>
<meta-data
android:name="android.service.quicksettings.ACTIVE_TILE"
android:value="true" />
</service>

<receiver android:name="app.intra.sys.AutoStarter" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
Expand Down
74 changes: 74 additions & 0 deletions Android/app/src/main/java/app/intra/sys/IntraTileService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions Android/app/src/main/java/app/intra/sys/IntraVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -176,6 +178,8 @@ public synchronized int onStartCommand(Intent intent, int flags, int startId) {

startForeground(SERVICE_ID, builder.getNotification());

updateQuickSettingsTile();

return START_REDELIVER_INTENT;
}

Expand Down Expand Up @@ -354,6 +358,8 @@ public void signalStopService(boolean userInitiated) {

stopVpnAdapter();
stopSelf();

updateQuickSettingsTile();
}

private VpnAdapter makeVpnAdapter() {
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 12 additions & 0 deletions Android/app/src/main/res/drawable/ic_tile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="48"
android:viewportHeight="48">
<path
android:pathData="M24,23.1l-21.55,22.37l43.1,0z"
android:fillColor="#FFFFFF"/>
<path
android:pathData="M40,19c0,3.95 -1.43,7.56 -3.81,10.35L24,17.12L11.81,29.35C9.43,26.56 8,22.95 8,19c0,-8.83 7.17,-16 16,-16S40,10.17 40,19z"
android:fillColor="#FFFFFF"/>
</vector>