-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recent versions of Android refuse to directly start a Service or a BroadcastReceiver, so always use an (invisible) Activity to receive intents. This fixes many cases where the confirmation dialog were not displayed: <https://github.com/Genymobile/gnirehtet/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3Anodialog>
- Loading branch information
Showing
6 changed files
with
116 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
app/src/main/java/com/genymobile/gnirehtet/AuthorizationActivity.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/genymobile/gnirehtet/GnirehtetActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.genymobile.gnirehtet; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.net.VpnService; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
|
||
/** | ||
* This (invisible) activity receives the {@link #ACTION_GNIREHTET_START START} and | ||
* {@link #ACTION_GNIREHTET_STOP} actions from the command line. | ||
* <p> | ||
* Recent versions of Android refuse to directly start a {@link android.app.Service Service} or a | ||
* {@link android.content.BroadcastReceiver BroadcastReceiver}, so actions are always managed by | ||
* this activity. | ||
*/ | ||
public class GnirehtetActivity extends Activity { | ||
|
||
private static final String TAG = GnirehtetActivity.class.getSimpleName(); | ||
|
||
public static final String ACTION_GNIREHTET_START = "com.genymobile.gnirehtet.START"; | ||
public static final String ACTION_GNIREHTET_STOP = "com.genymobile.gnirehtet.STOP"; | ||
|
||
public static final String EXTRA_DNS_SERVERS = "dnsServers"; | ||
public static final String EXTRA_ROUTES = "routes"; | ||
|
||
private static final int VPN_REQUEST_CODE = 0; | ||
|
||
private VpnConfiguration config; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
handleIntent(getIntent()); | ||
} | ||
|
||
private void handleIntent(Intent intent) { | ||
String action = intent.getAction(); | ||
Log.d(TAG, "Received request " + action); | ||
boolean finish = true; | ||
if (ACTION_GNIREHTET_START.equals(action)) { | ||
VpnConfiguration config = createConfig(intent); | ||
finish = startGnirehtet(config); | ||
} else if (ACTION_GNIREHTET_STOP.equals(action)) { | ||
stopGnirehtet(); | ||
} | ||
|
||
if (finish) { | ||
finish(); | ||
} | ||
} | ||
|
||
private static VpnConfiguration createConfig(Intent intent) { | ||
String[] dnsServers = intent.getStringArrayExtra(EXTRA_DNS_SERVERS); | ||
if (dnsServers == null) { | ||
dnsServers = new String[0]; | ||
} | ||
String[] routes = intent.getStringArrayExtra(EXTRA_ROUTES); | ||
if (routes == null) { | ||
routes = new String[0]; | ||
} | ||
return new VpnConfiguration(Net.toInetAddresses(dnsServers), Net.toCIDRs(routes)); | ||
} | ||
|
||
private boolean startGnirehtet(VpnConfiguration config) { | ||
Intent vpnIntent = VpnService.prepare(this); | ||
if (vpnIntent == null) { | ||
Log.d(TAG, "VPN was already authorized"); | ||
// we got the permission, start the service now | ||
GnirehtetService.start(this, config); | ||
return true; | ||
} | ||
|
||
Log.w(TAG, "VPN requires the authorization from the user, requesting..."); | ||
requestAuthorization(vpnIntent, config); | ||
return false; // do not finish now | ||
} | ||
|
||
private void stopGnirehtet() { | ||
GnirehtetService.stop(this); | ||
} | ||
|
||
private void requestAuthorization(Intent vpnIntent, VpnConfiguration config) { | ||
this.config = config; | ||
startActivityForResult(vpnIntent, VPN_REQUEST_CODE); | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) { | ||
GnirehtetService.start(this, config); | ||
} | ||
config = null; | ||
finish(); | ||
} | ||
} |
105 changes: 0 additions & 105 deletions
105
app/src/main/java/com/genymobile/gnirehtet/GnirehtetControlReceiver.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters