Dying
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.stardust.net;
|
||||
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/10.
|
||||
*/
|
||||
|
||||
public class AutoHttpURLConnection extends HttpURLConnection implements AutoCloseable {
|
||||
|
||||
private HttpURLConnection mHttpURLConnection;
|
||||
private InputStream mInputStream;
|
||||
private OutputStream mOutputStream;
|
||||
|
||||
|
||||
public AutoHttpURLConnection(URL url) throws IOException {
|
||||
super(url);
|
||||
mHttpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
connect();
|
||||
}
|
||||
|
||||
public AutoHttpURLConnection(String url) throws IOException {
|
||||
this(new URL(url));
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
try {
|
||||
mInputStream = super.getInputStream();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
return mInputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
mHttpURLConnection.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean usingProxy() {
|
||||
return mHttpURLConnection.usingProxy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws IOException {
|
||||
mHttpURLConnection.connect();
|
||||
}
|
||||
}
|
||||
10
common/src/main/java/com/stardust/util/Callback.java
Normal file
10
common/src/main/java/com/stardust/util/Callback.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.stardust.util;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/18.
|
||||
*/
|
||||
|
||||
public interface Callback<T> {
|
||||
|
||||
void call(T t);
|
||||
}
|
||||
107
common/src/main/java/com/stardust/util/DownloadTask.java
Normal file
107
common/src/main/java/com/stardust/util/DownloadTask.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.stardust.net.AutoHttpURLConnection;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/10.
|
||||
*/
|
||||
|
||||
public class DownloadTask extends AsyncTask<String, Integer, Boolean> {
|
||||
|
||||
public interface ProgressListener {
|
||||
void publishProgress(int i);
|
||||
}
|
||||
|
||||
public static class Download implements Callable<Boolean> {
|
||||
|
||||
private String mUrl;
|
||||
private String mPath;
|
||||
private ProgressListener mListener;
|
||||
private volatile boolean mCanceled = false;
|
||||
|
||||
public Download(String url, String path, ProgressListener listener) {
|
||||
mUrl = url;
|
||||
mPath = path;
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws IOException {
|
||||
try (AutoHttpURLConnection connection = new AutoHttpURLConnection(mUrl)) {
|
||||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
||||
return false;
|
||||
}
|
||||
int total = connection.getContentLength();
|
||||
InputStream input = connection.getInputStream();
|
||||
FileOutputStream output = new FileOutputStream(mPath);
|
||||
return download(input, output, total);
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
mCanceled = true;
|
||||
}
|
||||
|
||||
private boolean download(InputStream input, FileOutputStream output, int total) throws IOException {
|
||||
byte buffer[] = new byte[8192];
|
||||
long downloaded = 0;
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
if (mCanceled) {
|
||||
input.close();
|
||||
return false;
|
||||
}
|
||||
downloaded += read;
|
||||
if (total > 0)
|
||||
publishProgress((int) (downloaded * 100 / total));
|
||||
output.write(buffer, 0, read);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void publishProgress(int i) {
|
||||
if (mListener != null) {
|
||||
mListener.publishProgress(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Download mDownload;
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... params) {
|
||||
mDownload = new Download(params[0], params[1], new ProgressListener() {
|
||||
@Override
|
||||
public void publishProgress(int i) {
|
||||
DownloadTask.this.publishProgress(i);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return mDownload.call();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
mDownload = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
super.onCancelled();
|
||||
if (mDownload != null)
|
||||
mDownload.cancel();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class IntentUtil {
|
||||
|
||||
@@ -81,4 +83,11 @@ public class IntentUtil {
|
||||
public static boolean goToAppDetailSettings(Context context) {
|
||||
return goToAppDetailSettings(context, context.getPackageName());
|
||||
}
|
||||
|
||||
public static void installApk(Context context, String path) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
20
common/src/main/java/com/stardust/util/NetworkUtils.java
Normal file
20
common/src/main/java/com/stardust/util/NetworkUtils.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/9.
|
||||
*/
|
||||
|
||||
public class NetworkUtils {
|
||||
|
||||
public static boolean isWifiAvailable(Context context) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
|
||||
return activeNetInfo != null
|
||||
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
|
||||
}
|
||||
}
|
||||
35
common/src/main/java/com/stardust/util/ScreenMetrics.java
Normal file
35
common/src/main/java/com/stardust/util/ScreenMetrics.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.util.DisplayMetrics;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/26.
|
||||
*/
|
||||
|
||||
public class ScreenMetrics {
|
||||
|
||||
private static int screenHeight;
|
||||
private static int screenWidth;
|
||||
private static boolean initialized = false;
|
||||
|
||||
public static void initIfNeeded(Activity activity) {
|
||||
if (!initialized) {
|
||||
DisplayMetrics metrics = new DisplayMetrics();
|
||||
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
||||
screenHeight = metrics.heightPixels;
|
||||
screenWidth = metrics.widthPixels;
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getScreenHeight() {
|
||||
return screenHeight;
|
||||
}
|
||||
|
||||
public static int getScreenWidth() {
|
||||
return screenWidth;
|
||||
}
|
||||
}
|
||||
@@ -37,8 +37,12 @@ public class Shell {
|
||||
}
|
||||
|
||||
public Shell(boolean root) {
|
||||
this(root ? COMMAND_SU : COMMAND_SH);
|
||||
}
|
||||
|
||||
public Shell(String initialCommand) {
|
||||
try {
|
||||
mProcess = new ProcessBuilder(root ? COMMAND_SU : COMMAND_SH).redirectErrorStream(true).start();
|
||||
mProcess = new ProcessBuilder(initialCommand).redirectErrorStream(true).start();
|
||||
mCommandOutputStream = new DataOutputStream(mProcess.getOutputStream());
|
||||
mSucceedReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
|
||||
mErrorReader = new BufferedReader(new InputStreamReader(mProcess.getErrorStream()));
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.IdRes;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.RelativeLayout;
|
||||
@@ -26,5 +28,17 @@ public class ViewUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getScreenHeight(Activity activity) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||
return displayMetrics.heightPixels;
|
||||
}
|
||||
|
||||
public static int getScreenWidth(Activity activity) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||
return displayMetrics.widthPixels;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user