Skip to content

Commit

Permalink
Merge pull request #3 from yaoxinghuo/update-app
Browse files Browse the repository at this point in the history
  • Loading branch information
dlenski committed Nov 25, 2020
2 parents aa28eb0 + 28ff102 commit de93b96
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 19
compileSdkVersion 24
defaultConfig {
applicationId "app.openconnect"
minSdkVersion 14
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
android:targetSdkVersion="24" />

<!-- Copy the <permission> block to your app when using the REMOTE API. Otherwise OpenConnect
needs to be installed first -->
Expand Down Expand Up @@ -91,6 +91,7 @@
tools:ignore="ExportedActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>

<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down Expand Up @@ -120,6 +121,14 @@
</intent-filter>
</service>
-->
<service android:name=".QSTileService"
android:label="@string/app"
android:icon="@drawable/ic_launcher"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter>
</service>

<activity
android:permission="app.openconnect.REMOTE_API"
Expand Down
111 changes: 111 additions & 0 deletions app/src/main/java/app/openconnect/QSTileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2019.
*
* 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.openconnect;

import android.annotation.TargetApi;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import app.openconnect.core.OpenConnectManagementThread;
import app.openconnect.core.OpenVpnService;
import app.openconnect.core.VPNConnector;

/**
* @author Terry E-mail: yaoxinghuo at qq dot com
* @date 2019-6-2 18:33
* @description
*/
@TargetApi(24)
public class QSTileService extends TileService {

private static final String TAG = QSTileService.class.getName();

private int mConnectionState;
private VPNConnector mConn;

@Override
public void onStartListening() {
super.onStartListening();
mConn = new VPNConnector(this, true) {
@Override
public void onUpdate(OpenVpnService service) {
updateState(service);
}
};
}

@Override
public void onStopListening() {
super.onStopListening();

mConn.stopActiveDialog();
mConn.unbind();
}

@Override
public void onClick() {
super.onClick();

toggle();
}

private void updateState(OpenVpnService service) {
int newState = service.getConnectionState();

if (mConnectionState != newState) {
String tileLabel = null;
int tileState;
String profileName;
switch (newState) {
case OpenConnectManagementThread.STATE_CONNECTED:
tileState = Tile.STATE_ACTIVE;
tileLabel = getString(R.string.disconnect);
profileName = service.getReconnectName();
if (profileName != null) {
tileLabel = tileLabel + " " + profileName;
}
// Toast.makeText(this, getString(R.string.state_connected_to, service.profile.getName()), Toast.LENGTH_SHORT).show();
break;
case OpenConnectManagementThread.STATE_DISCONNECTED:
tileState = Tile.STATE_INACTIVE;
profileName = service.getReconnectName();
if (profileName != null) {
tileLabel = getString(R.string.reconnect_to, profileName);
}
break;
default:
tileLabel = service.getConnectionStateName();
tileState = Tile.STATE_UNAVAILABLE;
break;
}
mConnectionState = newState;

if (tileLabel == null) {
tileLabel = getString(R.string.app);
}

Tile tile = getQsTile();
tile.setState(tileState);
tile.setLabel(tileLabel);
Log.d(TAG, "set tile state: " + tileState + ", label: " + tileLabel);
tile.updateTile();
}
}

private void toggle() {
if (mConnectionState == OpenConnectManagementThread.STATE_CONNECTED) {
mConn.service.stopVPN();
} else if (mConnectionState == OpenConnectManagementThread.STATE_DISCONNECTED) {
mConn.service.startReconnectActivity(this);
}
}
}

0 comments on commit de93b96

Please sign in to comment.