Skip to content

Commit

Permalink
加入通知栏快捷开关
Browse files Browse the repository at this point in the history
  • Loading branch information
ndroi committed Oct 1, 2020
1 parent ceb2e1a commit cd170c7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Easy163/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "org.ndroi.easy163"
minSdkVersion 24
targetSdkVersion 29
versionCode 22
versionName "1.8.1"
versionCode 23
versionName "1.8.5"
}
buildTypes {
release {
Expand Down
11 changes: 10 additions & 1 deletion Easy163/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
<action android:name="android.net.VpnService" />
</intent-filter>
</service>
<service android:name=".ui.EasyTileService" />

<service
android:name=".ui.EasyTileService"
android:label="@string/app_name"
android:icon="@mipmap/icon"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<activity
android:name=".ui.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static protected Song generateSong(String url)
song.md5 = qqMusicMd5;
}
byte[] mp3Data = ReadStream.read(connection.getInputStream());
song.br = BitRate.Detect(mp3Data);
song.br = BitRate.detect(mp3Data);
}
} catch (IOException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class BitRate
table.put(0, map_2);
}

public static int Detect(byte[] bytes)
public static int detect(byte[] bytes)
{
int ptr = 0;
if (bytes[0] == 'f' && bytes[1] == 'L' && bytes[2] == 'a' && bytes[3] == 'C')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,93 @@
package org.ndroi.easy163.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.net.VpnService;
import android.os.IBinder;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import org.ndroi.easy163.vpn.LocalVPNService;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class EasyTileService extends TileService
{
@Override
public void onStartListening()
{
super.onStartListening();
Log.d("EasyTileService", "onStartListening");
Tile tile = getQsTile();
if (tile == null)
{
return;
}
if(LocalVPNService.getIsRunning())
{
tile.setState(Tile.STATE_ACTIVE);
tile.updateTile();
}else
{
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
}
}

@Override
public IBinder onBind(Intent intent)
{
TileService.requestListeningState(this, new ComponentName(this, EasyTileService.class));
return super.onBind(intent);
}

@Override
public void onClick()
{
super.onClick();
Tile tile = getQsTile();
if (tile == null)
{
return;
}
switch (tile.getState())
{
case Tile.STATE_ACTIVE:
{
stopVPN();
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
break;
}
case Tile.STATE_INACTIVE:
{
startVPN();
tile.setState(Tile.STATE_ACTIVE);
tile.updateTile();
break;
}
default:break;
}
}

private void startVPN()
{
if (VpnService.prepare(this) == null)
{
Intent intent = new Intent(this, LocalVPNService.class);
startService(intent);
} else
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityAndCollapse(intent);
}
}

private void stopVPN()
{
Intent intent = new Intent("control");
intent.putExtra("cmd", "stop");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.d("stopVPN", "try to stopVPN");
}
}
20 changes: 12 additions & 8 deletions Easy163/app/src/main/java/org/ndroi/easy163/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,37 @@
import org.ndroi.easy163.core.Local;
import org.ndroi.easy163.utils.EasyLog;
import org.ndroi.easy163.vpn.LocalVPNService;
import java.util.List;
import static androidx.appcompat.app.AlertDialog.Builder;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, ToggleButton.OnCheckedChangeListener
{
private static final int VPN_REQUEST_CODE = 0x0F;
private ToggleButton toggleButton = null;
private boolean isBroadcastReceived = false; // workaround for multi-receive
private static boolean isBroadcastReceived = false; // workaround for multi-receive
public static void resetBroadcastReceivedState()
{
isBroadcastReceived = false;
}

private BroadcastReceiver serviceReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(isBroadcastReceived) return;
if (isBroadcastReceived) return;
isBroadcastReceived = true;
boolean isServiceRunning = intent.getBooleanExtra("isRunning", false);
Log.d("MainActivity", "BroadcastReceiver service isRunning: " + isServiceRunning);
toggleButton.setChecked(isServiceRunning);
if(isServiceRunning)
{
EasyLog.log("Easy163 VPN 正在运行");
EasyLog.log("版本更新关注 Github Release");
}else
{
EasyLog.log("Easy163 VPN 停止运行");
}
isBroadcastReceived = true;
}
};

Expand Down Expand Up @@ -172,7 +178,6 @@ public void onClick(DialogInterface dialog, int which)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
isBroadcastReceived = false;
if (isChecked)
{
startVPN();
Expand All @@ -184,9 +189,8 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

private void syncServiceState()
{
Intent intent = new Intent("activity");
Intent intent = new Intent("control");
intent.putExtra("cmd", "check");
isBroadcastReceived = false;
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

Expand All @@ -201,7 +205,7 @@ private void startVPN()

private void stopVPN()
{
Intent intent = new Intent("activity");
Intent intent = new Intent("control");
intent.putExtra("cmd", "stop");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.d("stopVPN", "try to stopVPN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public class EasyLog

public static void log(String info)
{
logger.log(info);
if(logger != null)
{
logger.log(info);
}
}

public static void setTextView(TextView textView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
Expand All @@ -15,11 +16,14 @@
import android.os.Build;
import android.os.ParcelFileDescriptor;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.service.quicksettings.TileService;
import android.util.Log;
import org.ndroi.easy163.R;
import org.ndroi.easy163.core.Cache;
import org.ndroi.easy163.core.Local;
import org.ndroi.easy163.core.Server;
import org.ndroi.easy163.ui.EasyTileService;
import org.ndroi.easy163.ui.MainActivity;
import org.ndroi.easy163.utils.EasyLog;
import org.ndroi.easy163.vpn.bio.BioTcpHandler;
Expand Down Expand Up @@ -48,14 +52,19 @@ public class LocalVPNService extends VpnService
private BlockingQueue<Packet> deviceToNetworkTCPQueue;
private BlockingQueue<ByteBuffer> networkToDeviceQueue;
private ExecutorService executorService;
private Boolean isRunning = false;
private static Boolean isRunning = false;
private static Context context = null;

public static Context getContext()
{
return context;
}

public static Boolean getIsRunning()
{
return isRunning;
}

private BroadcastReceiver stopReceiver = new BroadcastReceiver()
{
@Override
Expand All @@ -81,7 +90,7 @@ public void onCreate()
super.onCreate();
context = getApplicationContext();
setupVPN();
LocalBroadcastManager.getInstance(this).registerReceiver(stopReceiver, new IntentFilter("activity"));
LocalBroadcastManager.getInstance(this).registerReceiver(stopReceiver, new IntentFilter("control"));
deviceToNetworkUDPQueue = new ArrayBlockingQueue<Packet>(1000);
deviceToNetworkTCPQueue = new ArrayBlockingQueue<Packet>(1000);
networkToDeviceQueue = new ArrayBlockingQueue<>(1000);
Expand All @@ -95,6 +104,8 @@ public void onCreate()
Cache.init();
Local.load();
isRunning = true;
sendState();
TileService.requestListeningState(this, new ComponentName(this, EasyTileService.class));
Log.i(TAG, "Easy163 VPN 启动");
}

Expand Down Expand Up @@ -165,20 +176,18 @@ private void setupVPN()
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
sendState();
return START_STICKY;
}

@Override
public void onDestroy()
{
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(stopReceiver);
executorService.shutdownNow();
cleanup();
isRunning = false;
sendState();
EasyLog.log("Easy163 VPN 停止运行");
TileService.requestListeningState(this, new ComponentName(this, EasyTileService.class));
Log.i(TAG, "Stopped");
}

Expand All @@ -192,9 +201,11 @@ private void cleanup()

private void sendState()
{
MainActivity.resetBroadcastReceivedState();
Intent replyIntent= new Intent("service");
replyIntent.putExtra("isRunning", isRunning);
LocalBroadcastManager.getInstance(this).sendBroadcast(replyIntent);
Log.i(TAG, "sendState");
}

private static void closeResources(Closeable... resources)
Expand Down

0 comments on commit cd170c7

Please sign in to comment.