Skip to content

Commit

Permalink
fix startPageImg bug & some bug
Browse files Browse the repository at this point in the history
  • Loading branch information
shenzhenjinma committed Jun 21, 2016
1 parent ddf96b2 commit f7f9f3c
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 97 deletions.
100 changes: 29 additions & 71 deletions src/main/java/com/reactnative/horsepush/HorsePush.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
Expand All @@ -32,15 +26,13 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

/**
* Created by techbin on 2016/3/18 0018.
*/
public class HorsePush {
private static HorsePush instanceHorsepush = null;
private static AlertDialog.Builder instanceAlertDialog = null;
private static boolean horsePushStartPageFinish = false;//这个页面是否被finish过
private static String HORSE_PUSH_WORK_PATH = "/mnt/sdcard/";//js文件夹路径,这个路径运行的时候会改变
private static String HORSE_PUSH_JS_FILE_NAME = "";//js名字
private static String HORSE_PUSH_ASSET_JS_NAME = "horse.push.js";//asset js名字
Expand All @@ -63,6 +55,7 @@ public class HorsePush {
private int appVersionCode = 0;//我的app版本
private String jsFileMd5 = "";//我的appmd5

private static long checkUpdateTime = 0;
private int[] requestRetryActionTime = {1500, 2000, 2500, 5000, 10000};//配置重试次数
private int requestRetryCount = 0;//重试次数
private boolean showUpdateDialog = false;//是否显示更新对话框
Expand Down Expand Up @@ -141,12 +134,28 @@ private void checkJS() {
checkUpdate();//更新
}

//是否可以更新
private static boolean isCanUpdate() {
long tempTime = System.currentTimeMillis();
if (checkUpdateTime == 0) {
checkUpdateTime = tempTime;
return false;
}//如果刚开始打开是10秒内是不可以更新的

if (tempTime - checkUpdateTime < 10000) {
return false;
}//间隔秒内不可以重复更新

checkUpdateTime = tempTime;

return instanceHorsepush != null;
}

public static void reCheckUpdate() {

if (instanceHorsepush == null || !horsePushStartPageFinish)
if (!isCanUpdate()) {
return;

}
instanceHorsepush.requestRetryCount = 0;
instanceHorsepush.showUpdateDialog = true;
instanceHorsepush.checkUpdate();
Expand Down Expand Up @@ -254,9 +263,9 @@ private void checkUpdate() {
params.addBodyParameter("channel", channel);
params.addBodyParameter("md5", jsFileMd5);
params.addBodyParameter("appversioncode", String.valueOf(appVersionCode));
params.addBodyParameter("isdev", isDev() ? "1" : "0");
params.addBodyParameter("isdev", HorsePushUtils.isDev() ? "1" : "0");
params.addBodyParameter("sdkint", String.valueOf(Build.VERSION.SDK_INT));
params.addBodyParameter("screensize", getScreenSize());
params.addBodyParameter("screensize", HorsePushUtils.getScreenSize(mContext));
params.addBodyParameter("brand", android.os.Build.BRAND);
params.addBodyParameter("model", android.os.Build.MODEL);
params.addBodyParameter("extradata", HorsePushModule.getExtraData(mContext));
Expand Down Expand Up @@ -423,7 +432,7 @@ private void downloadFileSuccess(int downTag) {
if (!showUpdateDialog) {
updateJs();
} else {//默认是不显示更新对话框的
if(instanceAlertDialog!=null)
if (instanceAlertDialog != null)
return;
getInstanceAlertDialog(mActivity)
.setMessage(jsVersionInfo)
Expand All @@ -434,7 +443,7 @@ public void onClick(DialogInterface dialog, int which) {
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
instanceAlertDialog=null;
instanceAlertDialog = null;
if (jsForceUpdate)//如果是强制更新点击取消会退出
System.exit(0);
}
Expand Down Expand Up @@ -528,24 +537,19 @@ private void installApk() {
}


//n秒后销毁启动页面
private void finishStartPage(int time) {
if (horsePushStartPageFinish)
return;
try {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
public void run() {
if (HorsePushStartPage.mActivity == null)
return;
HorsePushStartPage.mActivity.finish();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
horsePushStartPageFinish = true;
}
}, 1000);
Log.d("1111111111111111", "HorsePushStartPage.mActivity.finish()" );
}
}, time);
} catch (Exception e) {
}
} catch (Exception e) { }
}

/**
Expand Down Expand Up @@ -588,52 +592,6 @@ private void reApp(Context context) {
}


//是否为开发者
private boolean isDev() {
try {
File f = new File("/sdcard/horsepush.d");
if (!f.exists()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
}

//得到屏幕分辨率
private String getScreenSize() {
//first method
if (Build.VERSION.SDK_INT < 17) {
DisplayMetrics dm2 = mContext.getResources().getDisplayMetrics();
// 竖屏
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return dm2.widthPixels + "x" + dm2.heightPixels;
} else {// 横屏
return dm2.heightPixels + "x" + dm2.widthPixels;
}
} else {
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
try {
Method method = display.getClass().getMethod("getRealSize", Point.class);
method.invoke(display, size);
} catch (Exception e) {
e.printStackTrace();
}

int screenWidth = size.x;
int screenHeight = size.y;
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return screenWidth + "x" + screenHeight;
} else {
return screenHeight + "x" + screenWidth;
}
}
}


}


53 changes: 38 additions & 15 deletions src/main/java/com/reactnative/horsepush/HorsePushMd5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
Expand All @@ -16,22 +15,46 @@ public class HorsePushMd5 {
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private static MessageDigest messagedigest;

/* 获取一个文件的md5码 */
public static String getFileMD5String(File file) {
String md5Str="";
try {
messagedigest = MessageDigest.getInstance("MD5");
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
file.length());
messagedigest.update(byteBuffer);
// /* 获取一个文件的md5码 */
// public static String getFileMD5String(File file) {
// String md5Str="";
// try {
// messagedigest = MessageDigest.getInstance("MD5");
// FileInputStream in = new FileInputStream(file);
// FileChannel ch = in.getChannel();
// MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
// file.length());
// messagedigest.update(byteBuffer);
//
// md5Str = bufferToHex(messagedigest.digest());
// }catch (Exception e){ }
// return md5Str;
// }


md5Str = bufferToHex(messagedigest.digest());
}catch (Exception e){ }
return md5Str;
public static String getFileMD5String(File file) {
if (!file.isFile()) {
return "";
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
return "";
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}


/* 获取一个字符串的md5码 */
public static String getStringMD5String(String str) throws Exception {
messagedigest = MessageDigest.getInstance("MD5");
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/com/reactnative/horsepush/HorsePushStartPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Created by techbin on 2016/4/13 0013.
*/
public class HorsePushStartPage extends Activity {
public static Activity mActivity=null;
public static Activity mActivity = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -48,17 +48,26 @@ protected void onCreate(Bundle savedInstanceState) {
tempviewRoot.setPadding(0, height, 0, 0);
tempviewRoot.addView(pro);
tempviewRoot.setLayoutParams(layoutParams);
int imgSampleSize=7;
try{
int tempScreenSize=Integer.valueOf(HorsePushUtils.getScreenSize(getApplicationContext(),true));
if(tempScreenSize>=1080){
imgSampleSize=2;
}else if(tempScreenSize>=720){
imgSampleSize=3;
}else if(tempScreenSize>=540){
imgSampleSize=4;
}else if(tempScreenSize>=480){
imgSampleSize=5;
}
}catch (Exception e){ }


try {
// AssetManager assetManager = getAssets();
// InputStream is = assetManager.open("test.png");
// //以下注释掉的代码不靠谱.若采用,会有异常
// // Bitmap bitmap= BitmapFactory.decodeStream(is);
// Drawable dr = Drawable.createFromStream(is, null);
// imageView.setImageDrawable(dr);

String img = HorsePush.getStartPageImgPath(this);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inSampleSize = imgSampleSize;
Bitmap bm = BitmapFactory.decodeFile(img, options);
imageView.setImageBitmap(bm);

Expand All @@ -70,13 +79,14 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(layoutRoot);


}
}

private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime) > 2000){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
Expand All @@ -101,6 +111,8 @@ public boolean isNetworkConnected() {
}




}


Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/reactnative/horsepush/HorsePushUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.reactnative.horsepush;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import java.io.File;
import java.lang.reflect.Method;

/**
* Created by techbin on 16-6-15.
*/
public class HorsePushUtils {
//得到屏幕分辨率
public static String getScreenSize(Context mContext) {
String tempScreenSize = getScreenSize(mContext, false);
return tempScreenSize;
}


public static String getScreenSize(Context mContext, boolean isOnlyGetWidth) {
//first method
if (Build.VERSION.SDK_INT < 17) {
DisplayMetrics dm2 = mContext.getResources().getDisplayMetrics();
// 竖屏
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return isOnlyGetWidth ? dm2.widthPixels + "" : dm2.widthPixels + "x" + dm2.heightPixels;
} else {// 横屏
return isOnlyGetWidth ? dm2.heightPixels + "" : dm2.heightPixels + "x" + dm2.widthPixels;
}
} else {
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
try {
Method method = display.getClass().getMethod("getRealSize", Point.class);
method.invoke(display, size);
} catch (Exception e) {
e.printStackTrace();
}
int screenWidth = size.x;
int screenHeight = size.y;
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return isOnlyGetWidth ? screenWidth + "" : screenWidth + "x" + screenHeight;
} else {
return isOnlyGetWidth ? screenHeight + "" : screenHeight + "x" + screenWidth;
}
}
}


//是否为开发者
public static boolean isDev() {
try {
File f = new File("/sdcard/horsepush.d");
if (!f.exists()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
}



}

0 comments on commit f7f9f3c

Please sign in to comment.