提交 2c4a4ebb 编写于 作者: R Ruby Kurosawa

Package Delicated

上级 f4b138f8
package yuki.control.extended;
import android.app.Activity;
import java.io.*;
import java.net.*;
import java.util.Scanner;
/*Http Async Client*/
public final class HttpAsyncClient {
/**
* Async Get String
*
* @param Url URL
* @param activity Window
* @param get Response Handler
**/
public static void AsyncGetString(final String Url, final Activity activity, final OnGet get) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(Url);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
final String result = readInStream(in);
if (get != null && activity != null && get instanceof OnGetString) {
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
((OnGetString) get).Get(result);
}
}
);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
}).start();
}
private static String readInStream(InputStream in) {
Scanner scanner = new Scanner(in).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
/**
* Async Get File to local path
*
* @param Url File to download
* @param activity Window
* @param savePath local save path
* @param saveFileName local file name
* @param progress Progress Changed Handler
* @param get Download Finished Handler
**/
public static void AsyncGetFile(final String Url, final Activity activity, final String savePath,
final String saveFileName, final OnProgress progress, final OnGet get) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(Url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
final int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdirs();
}
final String apkFile = savePath + "/" + saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
final byte[] buf = new byte[1024];
//double progressu = 0;
do {
int numread = is.read(buf);
count += numread;
final int proceed = count;
final double progressu = (double) (((float) count / length) * 100);
if (activity != null && progress != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progress.Progress(proceed, length, progressu);
}
});
}
if (numread <= 0) {
//mHandler.sendEmptyMessage(DOWN_OVER);
if (activity != null && get != null && get instanceof OnGetFile) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((OnGetFile) get).Get(apkFile);
}
});
}
break;
}
fos.write(buf, 0, numread);
} while (true);
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* Async Get File to local path (filename auto-detected)
*
* @param Url File to download
* @param activity Window
* @param savePath local save path
* @param progress Progress Changed Handler
* @param get Download Finished Handler
**/
public static void AsyncGetFile(final String Url, final Activity activity, final String savePath
, final OnProgress progress, final OnGet get) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(Url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
final int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdirs();
}
final String saveFileName = URLDecoder.decode(url.getFile(), "UTF-8");
final String apkFile = savePath + "/" + saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
final byte[] buf = new byte[1024];
//double progressu = 0;
do {
int numread = is.read(buf);
count += numread;
final int proceed = count;
final double progressu = (double) (((float) count / length) * 100);
if (activity != null && progress != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progress.Progress(proceed, length, progressu);
}
});
}
if (numread <= 0) {
//mHandler.sendEmptyMessage(DOWN_OVER);
if (activity != null && get != null && get instanceof OnGetFile) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((OnGetFile) get).Get(apkFile);
}
});
}
break;
}
fos.write(buf, 0, numread);
} while (true);
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public static void AsyncPostString(final String Url,final Activity activity,final String ContentType,final String rawData,final OnPost post){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(Url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",ContentType);
urlConnection.setRequestProperty("Connection","Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setUseCaches(false);
OutputStream out=urlConnection.getOutputStream();
out.write(rawData.getBytes("UTF-8"));
out.flush();
out.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
final String result = readInStream(in);
if (post != null && activity != null && post instanceof OnPostString) {
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
((OnPostString) post).Post(result);
}
}
);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
}).start();
}
public static void AsyncRESTString(final String Url,final Activity activity,final String RESTMethod,final String ContentType,final String rawData,final OnREST post){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(Url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod(RESTMethod);
urlConnection.setRequestProperty("Content-Type",ContentType);
urlConnection.setRequestProperty("Connection","Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setUseCaches(false);
OutputStream out=urlConnection.getOutputStream();
out.write(rawData.getBytes("UTF-8"));
out.flush();
out.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
final String result = readInStream(in);
if (post != null && activity != null && post instanceof OnRESTString) {
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
((OnRESTString) post).REST(result);
}
}
);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
}).start();
}
}
package yuki.control.extended;
/*OnGet Response Handler*/
public interface OnGet {
}
package yuki.control.extended;
/*On Get Bytes Response Handler*/
public interface OnGetBytes extends OnGet {
/**
* Response Method
*
* @param data Response DataBytes
*/
public void Get(byte[] data);
}
package yuki.control.extended;
/**
* On Get File Handler
*/
public interface OnGetFile extends OnGet {
/**
* Response Method
*
* @param fileName FilePath saved
*/
public void Get(String fileName);
}
package yuki.control.extended;
/**
* On Get String Handler
*/
public interface OnGetString extends OnGet {
/**
* Response Method
*
* @param data String Data
*/
public void Get(String data);
}
package yuki.control.extended;
/**
* Created by Akeno on 2016/08/31.
*/
public interface OnPost {
}
package yuki.control.extended;
/**
* Created by Akeno on 2016/08/31.
*/
public interface OnPostString extends OnPost {
public void Post(String data);
}
package yuki.control.extended;
/**
* Progress Changed Handler
*/
public interface OnProgress {
/**
* Changed Handler
*
* @param proceed Download bytes count
* @param total Total bytes count
* @param progress Download Percentage
**/
public void Progress(int proceed, int total, double progress);
}
package yuki.control.extended;
/**
* Created by Akeno on 2017/08/04.
*/
public interface OnREST {
}
package yuki.control.extended;
/**
* Created by Akeno on 2017/08/04.
*/
public interface OnRESTString extends OnREST {
public void REST(String data);
}
package yuki.pm.extended;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import org.json.JSONObject;
import yuki.control.extended.HttpAsyncClient;
import yuki.control.extended.OnGetString;
/**
* Auto Update Class
*/
public final class AutoUpdateManager {
private Context mContext = null;
private Activity mActivity = null;
private Handler mUpdateHandler = null;
public AutoUpdateManager(Context context, Activity activity, Handler updateHandler) {
mContext = context;
mActivity = activity;
mUpdateHandler = updateHandler;
}
/**
* Check Network Info
*
* @param sharepref Config File
* @param key Config Key
*/
public void NetworkEnvironmentCheck(final String sharepref, final String key) {
final SharedPreferences mobilenetwork = mContext.getSharedPreferences(sharepref, Context.MODE_PRIVATE);
ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
boolean lte = false;
if (NetworkManager.GetNetworkType(mContext) != NetworkManager.NO_NETWORK) {
if (NetworkManager.GetNetworkType(mContext) == ConnectivityManager.TYPE_MOBILE) {
lte = true;
}
} else {
AlertDialog.Builder b = new AlertDialog.Builder(mActivity);
b.setTitle("网络不可用");
b.setMessage("没有可用的网络连接,程序将会关闭");
b.setPositiveButton("关闭程序", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
b.show();
return;
}
if ((mobilenetwork == null || !mobilenetwork.contains(key) && lte)) {
AlertDialog.Builder b = new AlertDialog.Builder(mActivity);
b.setTitle("移动网络连接");
b.setMessage("使用移动网络连接会产生流量费用,仍然通过移动网络打开程序?");
b.setPositiveButton("允许移动网络连接", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor e = mobilenetwork.edit();
e.putString("lte", "");
e.apply();
}
});
b.setNeutralButton("允许本次连接", null);
b.setNegativeButton("退出程序", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
b.create().show();
}
}
/**
* Check Update Via Update.JSON
*
* @param updateServer Update Server
* @param pidUpdate Package ID Change Handler
* @param pmUpdate Package Update Found Handler
*/
public void UpdateCheck(final String updateServer, final UpdateClickListener pidUpdate,
final UpdateClickListener pmUpdate) {
UpdateCheck(updateServer, "update.json", pidUpdate, pmUpdate);
}
/**
* Check Update Via Network
*
* @param updateServer UpdateServer
* @param updateFile Update Config File
* @param pidUpdate Package ID Change Handler
* @param pmUpdate Package Update Found Handler
*/
public void UpdateCheck(final String updateServer, final String updateFile, final UpdateClickListener pidUpdate,
final UpdateClickListener pmUpdate) {
HttpAsyncClient.AsyncGetString(updateServer + "/" + updateFile, mActivity, new OnGetString() {
@Override
public void Get(String data) {
try {
final UpdateInfo newUpdateInfo = new UpdateInfo(new JSONObject(data));
final UpdateInfo oldUpdateInfo = new UpdateInfo(pm.GetCurrentVersionName(mContext),
pm.GetCurrentVersionCode(mContext), pm.GetCurrentPackageName(mContext));
if (oldUpdateInfo.getPackageName() != newUpdateInfo.getPackageName()) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("程序包标识符更新");
builder.setMessage("程序包的标识符更新,更新后需手动卸载旧版本。");
builder.setNegativeButton("取消更新", null);
pidUpdate.setInfo(newUpdateInfo);
builder.setPositiveButton("立即更新", pidUpdate);
builder.create().show();
}
});
} else if (oldUpdateInfo.getVersionCode() < newUpdateInfo.getVersionCode()) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("程序包更新可用");
builder.setMessage("有新的程序包更新可供安装\n" + "新版本: " +
newUpdateInfo.getVersionName() + "(" + newUpdateInfo.getVersionCode() + ")" +
"\n更新时间: " + newUpdateInfo.getUpdateTime() + "\n更新人:\n" + newUpdateInfo.getUpdater()
+ "\n更新内容:\n" + newUpdateInfo.getUpdateInfo());
builder.setNegativeButton("取消更新", null);
pmUpdate.setInfo(newUpdateInfo);
builder.setPositiveButton("立即更新", pmUpdate);
builder.create().show();
}
});
}
} catch (Exception e) {
final String Data = data;
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("更新出错");
builder.setMessage("无法解析更新数据包\n" + Data);
builder.setNegativeButton("关闭", null);
builder.create().show();
}
});
}
}
});
}
}
package yuki.pm.extended;
/**
* Created by Akeno on 2017/01/05.
*/
public class KeyValuePair<K, V> {
public K getKey() {
return Key;
}
public void setKey(K key) {
Key = key;
}
public V getValue() {
return Value;
}
public void setValue(V value) {
Value = value;
}
private K Key;
private V Value;
}
package yuki.pm.extended;
import java.util.Comparator;
/**
* Created by Akeno on 2017/01/05.
*/
public class KeyValuePairComparator<K,V> implements Comparator {
@Override
public int compare(Object lhs, Object rhs) {
return ((KeyValuePair<K,V>)lhs).getKey().toString().compareTo(((KeyValuePair<K,V>)rhs).getKey().toString());
}
}
package yuki.pm.extended;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;