Skip to content

Commit

Permalink
Support SDK 17 for stub APK
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Dec 27, 2018
1 parent 523e662 commit 920b60d
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 31 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {

defaultConfig {
applicationId "com.topjohnwu.magisk"
minSdkVersion 21
targetSdkVersion rootProject.ext.compileSdkVersion
vectorDrawables.useSupportLibrary = true
}

signingConfigs {
Expand Down Expand Up @@ -41,6 +41,7 @@ android {

productFlavors {
full {
minSdkVersion 21
versionName configProps['appVersion']
versionCode configProps['appVersionCode'] as Integer
javaCompileOptions {
Expand All @@ -50,6 +51,7 @@ android {
}
}
stub {
minSdkVersion 17
versionCode 1
versionName "stub"
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/full/java/com/topjohnwu/magisk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.topjohnwu.magisk.fragments.ReposFragment;
import com.topjohnwu.magisk.fragments.SettingsFragment;
import com.topjohnwu.magisk.fragments.SuperuserFragment;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.Shell;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -124,7 +124,7 @@ public void checkHideSection() {
menu.findItem(R.id.magiskhide).setVisible(Shell.rootAccess() &&
app.prefs.getBoolean(Const.Key.MAGISKHIDE, false));
menu.findItem(R.id.modules).setVisible(Shell.rootAccess() && Data.magiskVersionCode >= 0);
menu.findItem(R.id.downloads).setVisible(Download.checkNetworkStatus(this)
menu.findItem(R.id.downloads).setVisible(Networking.checkNetworkStatus(this)
&& Shell.rootAccess() && Data.magiskVersionCode >= 0);
menu.findItem(R.id.log).setVisible(Shell.rootAccess());
menu.findItem(R.id.superuser).setVisible(Utils.showSuperUser());
Expand Down
4 changes: 2 additions & 2 deletions app/src/full/java/com/topjohnwu/magisk/SplashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.topjohnwu.magisk.components.Notifications;
import com.topjohnwu.magisk.receivers.ShortcutReceiver;
import com.topjohnwu.magisk.utils.AppUtils;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.Shell;

public class SplashActivity extends BaseActivity {
Expand Down Expand Up @@ -56,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Setup shortcuts
sendBroadcast(new Intent(this, ClassMap.get(ShortcutReceiver.class)));

if (Download.checkNetworkStatus(this)) {
if (Networking.checkNetworkStatus(this)) {
// Fire update check
CheckUpdates.check();
// Repo update check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.topjohnwu.magisk.components.MagiskInstallDialog;
import com.topjohnwu.magisk.components.ManagerInstallDialog;
import com.topjohnwu.magisk.components.UninstallDialog;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;

Expand Down Expand Up @@ -169,7 +169,7 @@ public void onRefresh() {
shownDialog = false;

// Trigger state check
if (Download.checkNetworkStatus(app)) {
if (Networking.checkNetworkStatus(app)) {
CheckUpdates.check();
} else {
mSwipeRefreshLayout.setRefreshing(false);
Expand Down Expand Up @@ -212,7 +212,7 @@ private boolean hasGms() {
private void updateUI() {
((MainActivity) requireActivity()).checkHideSection();

boolean hasNetwork = Download.checkNetworkStatus(app);
boolean hasNetwork = Networking.checkNetworkStatus(app);
boolean hasRoot = Shell.rootAccess();

magiskUpdate.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.AppUtils;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.DownloadApp;
import com.topjohnwu.magisk.utils.FingerprintHelper;
import com.topjohnwu.magisk.utils.PatchAPK;
import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.Shell;

import java.io.IOException;
Expand Down Expand Up @@ -155,7 +155,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
if (app.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
generalCatagory.removePreference(restoreManager);
} else {
if (!Download.checkNetworkStatus(app))
if (!Networking.checkNetworkStatus(app))
generalCatagory.removePreference(restoreManager);
generalCatagory.removePreference(hideManager);
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package="com.topjohnwu.magisk">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

Expand Down
15 changes: 0 additions & 15 deletions app/src/main/java/com/topjohnwu/magisk/utils/Download.java

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions app/src/stub/java/com/topjohnwu/magisk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Bundle;

import com.topjohnwu.magisk.utils.APKInstall;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.net.Networking;
import com.topjohnwu.net.ResponseListener;

Expand All @@ -32,7 +31,8 @@ private void dlAPK() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Download.checkNetworkStatus(this)) {
Networking.init(this);
if (Networking.checkNetworkStatus(this)) {
Networking.get(URL)
.setErrorHandler(((conn, e) -> finish()))
.getAsJSONObject(new JSONLoader());
Expand Down
4 changes: 4 additions & 0 deletions app/src/stub/res/values/drawables.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="ic_launcher">@android:drawable/sym_def_app_icon</drawable>
</resources>
2 changes: 2 additions & 0 deletions core/src/main/java/com/topjohnwu/core/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.topjohnwu.core.database.RepoDatabaseHelper;
import com.topjohnwu.core.utils.LocaleManager;
import com.topjohnwu.core.utils.RootUtils;
import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.ContainerApp;
import com.topjohnwu.superuser.Shell;

Expand Down Expand Up @@ -46,6 +47,7 @@ protected void attachBaseContext(Context base) {
mDB = new MagiskDB(this);
repoDB = new RepoDatabaseHelper(this);

Networking.init(this);
LocaleManager.setLocale(this);
Data.loadConfig();
}
Expand Down
5 changes: 4 additions & 1 deletion net/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.topjohnwu.net" />
package="com.topjohnwu.net">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
29 changes: 28 additions & 1 deletion net/src/main/java/com/topjohnwu/net/Networking.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.topjohnwu.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;

import javax.net.ssl.HttpsURLConnection;

public class Networking {

Expand All @@ -30,4 +35,26 @@ public static Request get(String url) {
return request(url, "GET");
}

public static void init(Context context) {
try {
// Try installing new SSL provider from Google Play Service
Context gms = context.createPackageContext("com.google.android.gms",
Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
gms.getClassLoader()
.loadClass("com.google.android.gms.common.security.ProviderInstallerImpl")
.getMethod("insertProvider", Context.class)
.invoke(null, context);
} catch (Exception e) {
// Failed to update SSL provider, use NoSSLv3SocketFactory on SDK < 21
if (Build.VERSION.SDK_INT < 21)
HttpsURLConnection.setDefaultSSLSocketFactory(new NoSSLv3SocketFactory());
}
}

public static boolean checkNetworkStatus(Context context) {
ConnectivityManager manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
70 changes: 70 additions & 0 deletions net/src/main/java/com/topjohnwu/net/NoSSLv3SocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.topjohnwu.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

class NoSSLv3SocketFactory extends SSLSocketFactory {

private final static SSLSocketFactory base = HttpsURLConnection.getDefaultSSLSocketFactory();

@Override
public String[] getDefaultCipherSuites() {
return base.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return base.getSupportedCipherSuites();
}

private Socket createSafeSocket(Socket socket) {
if (socket instanceof SSLSocket)
return new SSLSocketWrapper((SSLSocket) socket) {
@Override
public void setEnabledProtocols(String[] protocols) {
List<String> proto = new ArrayList<>(Arrays.asList(getSupportedProtocols()));
proto.remove("SSLv3");
super.setEnabledProtocols(proto.toArray(new String[0]));
}
};
return socket;
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return createSafeSocket(base.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket() throws IOException {
return createSafeSocket(base.createSocket());
}

@Override
public Socket createSocket(String host, int port) throws IOException {
return createSafeSocket(base.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
return createSafeSocket(base.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return createSafeSocket(base.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return createSafeSocket(base.createSocket(address, port, localAddress, localPort));
}
}
Loading

0 comments on commit 920b60d

Please sign in to comment.