Forráskód Böngészése

添加zip注释的增删查测试功能

zengjiebin 7 éve
szülő
commit
5e0edcd67f

+ 2 - 0
app/src/main/AndroidManifest.xml

@@ -480,6 +480,8 @@
             android:screenOrientation="portrait"/>
         <activity android:name="com.sheep.gamegroup.view.activity.ActInstallApkList"
             android:screenOrientation="portrait"/>
+        <activity android:name="com.sheep.gamegroup.view.activity.ActSheepApkList"
+            android:screenOrientation="portrait"/>
         <activity android:name="com.sheep.gamegroup.view.activity.ActFindGame"
             android:screenOrientation="portrait"/>
         <activity android:name="com.sheep.gamegroup.view.activity.ActFindInformation"

+ 2 - 11
app/src/main/java/com/kfzs/duanduan/BaseCompatFragment.java

@@ -109,7 +109,7 @@ public abstract class BaseCompatFragment extends Fragment {
     public void onResume() {
         super.onResume();
         if(getUserVisibleHint()){
-            UMConfigUtils.onVisibilityChangedToUser(true, false,TAG);
+            UMConfigUtils.onVisibilityChangedToUser(true, TAG);
         }
     }
 
@@ -117,7 +117,7 @@ public abstract class BaseCompatFragment extends Fragment {
     public void onPause() {
         super.onPause();
         if(getUserVisibleHint()){
-            UMConfigUtils.onVisibilityChangedToUser(false, false,TAG);
+            UMConfigUtils.onVisibilityChangedToUser(false, TAG);
         }
     }
 
@@ -153,15 +153,6 @@ public abstract class BaseCompatFragment extends Fragment {
         }
     }
 
-    @Override
-    public void setUserVisibleHint(boolean isVisibleToUser) {
-        super.setUserVisibleHint(isVisibleToUser);
-
-        if(isResumed()){
-            UMConfigUtils.onVisibilityChangedToUser(isVisibleToUser,true,TAG);
-        }
-    }
-
 
 
     /**

+ 5 - 1
app/src/main/java/com/kfzs/duanduan/fragment/FgtFind.java

@@ -100,7 +100,11 @@ public class FgtFind extends BaseCompatFragment {
             }
         });
         pager.setCurrentItem(position);
-        reflex(indicator);
+        try {
+            reflex(indicator);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
     private int position = 0;//默认选中第一个
 

+ 37 - 0
app/src/main/java/com/sheep/gamegroup/model/entity/ApkInfo.java

@@ -0,0 +1,37 @@
+package com.sheep.gamegroup.model.entity;
+
+import java.io.File;
+
+/**
+ * Created by realicing on 2018/7/18.
+ * realicing@sina.com
+ */
+public class ApkInfo {
+    private String comment;
+    private AppInfo appInfo;
+    private File file;
+
+    public String getComment() {
+        return comment;
+    }
+
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+
+    public AppInfo getAppInfo() {
+        return appInfo;
+    }
+
+    public void setAppInfo(AppInfo appInfo) {
+        this.appInfo = appInfo;
+    }
+
+    public File getFile() {
+        return file;
+    }
+
+    public void setFile(File file) {
+        this.file = file;
+    }
+}

+ 6 - 2
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -14,7 +14,6 @@ import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.TextView;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.bumptech.glide.Glide;
 import com.kfzs.duanduan.ActMain;
@@ -29,6 +28,7 @@ import com.sheep.gamegroup.model.entity.UserEntity;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.usage.AppUsageManager;
 import com.sheep.gamegroup.view.activity.ActInstallApkList;
+import com.sheep.gamegroup.view.activity.ActSheepApkList;
 import com.sheep.gamegroup.view.activity.ActWeb;
 import com.sheep.gamegroup.view.dialog.DialogNewbieTaskList;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
@@ -283,7 +283,7 @@ public class TestUtil {
      * @param activity
      */
     public static void test(final Activity activity) {
-        final String[] items = {"测试可用金额","复制token","复制打点数据","游戏搜索","游戏帐号","游戏代充","尝试开启第三方应用使用情况","测试代理页面","第三方应用使用情况",
+        final String[] items = {"复制token","测试apk的渠道","测试可用金额","复制打点数据","游戏搜索","游戏帐号","游戏代充","尝试开启第三方应用使用情况","测试代理页面","第三方应用使用情况",
                 "开启第三方应用使用情况","h5跳转","新手对话框","md5","空间不足提示框",
                 "显示已经安装应用列表","复制faq地址","复制代理地址","复制世界杯地址","任务游戏列表","世界杯活动","交通银行信用卡测试",
                 "浦发银行信用卡测试", "测试游戏模块","打卡成功提示","定向货币详情","进入绑定身份认证界面时的提示","提交身份认证时的提示", "检查标签",
@@ -294,6 +294,10 @@ public class TestUtil {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         switch (items[which]) {
+                            case "测试apk的渠道":
+                                Intent intent = new Intent(activity, ActSheepApkList.class);
+                                activity.startActivity(intent);
+                                break;
                             case "测试可用金额":
                                 CommonUtil.getInstance()
                                         .getVoucherAndRatio(activity, 115+"", new Action1<BaseMessage>() {

+ 1 - 2
app/src/main/java/com/sheep/gamegroup/util/UMConfigUtils.java

@@ -366,10 +366,9 @@ public class UMConfigUtils {
      * 当Fragment对用户的可见性发生了改变的时候就会回调此方法
      *
      * @param isVisibleToUser                      true:用户能看见当前Fragment;false:用户看不见当前Fragment
-     * @param isHappenedInSetUserVisibleHintMethod true:本次回调发生在setUserVisibleHintMethod方法里;false:发生在onResume或onPause方法里
      * @param tag                                  fragment 名字
      */
-    public static void onVisibilityChangedToUser(boolean isVisibleToUser, boolean isHappenedInSetUserVisibleHintMethod, String tag) {
+    public static void onVisibilityChangedToUser(boolean isVisibleToUser, String tag) {
         if (isVisibleToUser) {
             if (tag != null) {
                 //umeng fragment

+ 186 - 0
app/src/main/java/com/sheep/gamegroup/util/ZipChannelUtil.java

@@ -0,0 +1,186 @@
+package com.sheep.gamegroup.util;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.text.TextUtils;
+
+import java.io.DataOutput;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Created by realicing on 2018/7/18.
+ * realicing@sina.com
+ */
+public class ZipChannelUtil {
+    // ZIP文件注释长度字段和MAGIC的字节数
+    static final int SHORT_LENGTH = 2;
+    //注释字符编码
+    static final String UTF_8 = "UTF-8";
+    // 文件最后用于定位的MAGIC字节
+    static final byte[] MAGIC = new byte[]{0x21, 0x4b, 0x46, 0x5a, 0x53, 0x21}; //!KFZS!
+
+    /**
+     * 写入渠道号
+     * @param file 执行该方法前应该首先确定该文件存在,否则可能会得到异想不到的结果
+     * @param comment
+     * @throws IOException
+     */
+    public static void writeQUA(File file, String comment) throws IOException {
+        byte[] data = comment.getBytes(UTF_8);
+        final RandomAccessFile raf = new RandomAccessFile(file, "rw");// rw 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件
+        //定位到文件有效内容的末尾(文件长度-注释长度)
+        raf.seek(file.length() - SHORT_LENGTH);
+        //写入注释字节数{注释字节数+2(MAGIC长度说明)+MAGIC长度}
+        writeShort(data.length + SHORT_LENGTH + MAGIC.length, raf);
+        //写入注释内容
+        writeBytes(data, raf);
+        //写入MAGIC字节数
+        writeShort(data.length, raf);
+        //写入MAGIC
+        writeBytes(MAGIC, raf);
+        raf.close();
+    }
+
+    private static void writeBytes(byte[] data, DataOutput out) throws IOException {
+        out.write(data);
+    }
+
+    private static void writeShort(int i, DataOutput out) throws IOException {
+        ByteBuffer bb = ByteBuffer.allocate(SHORT_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
+        bb.putShort((short) i);
+        out.write(bb.array());
+    }
+
+
+
+
+
+    //读取源apk的路径
+    public static String getSourceApkPath(Context context, String packageName) {
+        if (TextUtils.isEmpty(packageName))
+            return null;
+        try {
+            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
+            return appInfo.sourceDir;
+        } catch (PackageManager.NameNotFoundException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    //读取渠道号
+    public static String readQUA(File file) throws IOException {
+        RandomAccessFile raf = null;
+        try {
+            raf = new RandomAccessFile(file, "r");
+            long index = raf.length();
+            byte[] buffer = new byte[MAGIC.length];
+            index -= MAGIC.length;
+            //定位到MAGIC处
+            raf.seek(index);
+            //读取MAGIC
+            raf.readFully(buffer);
+            //判断文件末尾是否存在MAGIC字符
+            if (isMagicMatched(buffer)) {
+                index -= SHORT_LENGTH;
+                raf.seek(index);
+                //读取渠道号长度
+                int length =  readShort(raf);
+                if (length > 0) {
+                    index -= length;
+                    raf.seek(index);
+                    //读取渠道号
+                    byte[] bytesComment = new byte[length];
+                    raf.readFully(bytesComment);
+                    return new String(bytesComment, UTF_8);
+                }
+            }
+        } finally {
+            if (raf != null) {
+                raf.close();
+            }
+        }
+        return "";
+    }
+    //是否已经添加渠道号
+    public static boolean isAddQUA(File file) throws IOException {
+        RandomAccessFile raf = null;
+        try {
+            raf = new RandomAccessFile(file, "r");
+            long index = raf.length();
+            byte[] buffer = new byte[MAGIC.length];
+            index -= MAGIC.length;
+            //定位到MAGIC处
+            raf.seek(index);
+            //读取MAGIC
+            raf.readFully(buffer);
+            //判断文件末尾是否存在MAGIC字符
+            return isMagicMatched(buffer);
+        } finally {
+            if (raf != null) {
+                raf.close();
+            }
+        }
+    }
+
+    private static int readShort(RandomAccessFile raf) throws IOException {
+        byte[] buffer = new byte[SHORT_LENGTH];
+        raf.read(buffer);
+        return buffer[0] + buffer[1]*16*16;//0a00中 0a 10 ,00 0 -> 10+0*256 = 10
+    }
+
+    //判断是否存在渠道号
+    private static boolean isMagicMatched(byte[] buffer) {
+        if (buffer.length != MAGIC.length) {
+            return false;
+        }
+        for (int i = 0; i < MAGIC.length; ++i) {
+            if (buffer[i] != MAGIC[i]) {
+                return false;
+            }
+        }
+        return true;
+    }
+    //删除渠道
+    public static void delQUA(File file) throws IOException {
+        RandomAccessFile raf = null;
+        try {
+            raf = new RandomAccessFile(file, "rw");
+            long index = raf.length();
+            byte[] buffer = new byte[MAGIC.length];
+            index -= MAGIC.length;
+            //定位到MAGIC处
+            raf.seek(index);
+            //读取MAGIC
+            raf.readFully(buffer);
+            //判断文件末尾是否存在MAGIC字符
+            if (isMagicMatched(buffer)) {
+                index -= SHORT_LENGTH;
+                raf.seek(index);
+                //读取注释中渠道长度
+                int length =  readShort(raf);
+                if (length > 0) {
+                    index -= length;
+                    //跳过注释
+                    raf.seek(index);
+                    //跳过注释长度
+                    index -= SHORT_LENGTH;
+                    raf.seek(index);
+                    byte[] NO_COMMENT = new byte[]{0x00, 0x00};
+                    writeBytes(NO_COMMENT, raf);
+                    long newLength = index + NO_COMMENT.length;
+                    raf.setLength(newLength);
+                }
+            }
+        } finally {
+            if (raf != null) {
+                raf.close();
+            }
+        }
+    }
+}

+ 210 - 0
app/src/main/java/com/sheep/gamegroup/view/activity/ActSheepApkList.java

@@ -0,0 +1,210 @@
+package com.sheep.gamegroup.view.activity;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.graphics.drawable.Drawable;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
+import android.text.format.Formatter;
+import android.view.View;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import com.arialyy.aria.util.CommonUtil;
+import com.sheep.gamegroup.absBase.BaseActivity;
+import com.sheep.gamegroup.model.entity.ApkInfo;
+import com.sheep.gamegroup.model.entity.AppInfo;
+import com.sheep.gamegroup.util.ListUtil;
+import com.sheep.gamegroup.util.ViewHolder;
+import com.sheep.gamegroup.util.ViewUtil;
+import com.sheep.gamegroup.util.ZipChannelUtil;
+import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.SheepApp;
+import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import butterknife.BindView;
+import butterknife.OnClick;
+
+import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
+
+/**
+ * Created by realicing on 2018/6/19.
+ * realicing@sina.com
+ */
+public class ActSheepApkList extends BaseActivity {
+    @Override
+    protected int getLayoutId() {
+        return R.layout.act_user_label_list;
+    }
+
+
+    @BindView(R.id.user_label_list)
+    RecyclerView user_label_list;
+
+    @BindView(R.id.user_label_commit_tv)
+    TextView user_label_commit_tv;
+
+    @Override
+    public void initView() {
+        TitleBarUtils
+                .getInstance()
+                .setTitle(this, "Sheep目录Apk渠道测试")
+                .setTitleFinish(this);
+        user_label_commit_tv.setVisibility(View.GONE);
+    }
+
+    @Override
+    public void initListener() {
+
+    }
+
+    @Override
+    public void initData() {
+        loadData(getList());
+    }
+
+    private List<ApkInfo> getList() {
+        List<ApkInfo> apkInfoList = ListUtil.emptyList();
+
+        File dir = new File(DIR);
+        File[] files = dir.listFiles();
+        if(files != null)
+            for (File file : files) {
+                apkInfoList.add(getAppInfo(getApplicationContext(), file));
+            }
+        return apkInfoList;
+    }
+    public static ApkInfo getAppInfo(Context context, File file) {
+        ApkInfo apkInfo = new ApkInfo();
+        PackageManager packageManager = context.getPackageManager();
+        String absolutePath = file.getAbsolutePath();
+        PackageInfo pkgInfo = getPackageInfo(context, absolutePath);
+        if (pkgInfo == null) {
+            return apkInfo;
+        }
+        pkgInfo.applicationInfo.sourceDir = absolutePath;
+        pkgInfo.applicationInfo.publicSourceDir = absolutePath;
+
+        AppInfo appInfo = new AppInfo();
+        //程序包名
+        String packageName = pkgInfo.packageName;
+        appInfo.setPackageName(packageName);
+        //获取到图标
+        Drawable icon = pkgInfo.applicationInfo.loadIcon(packageManager);
+        appInfo.setIcon(icon);
+        //获取到应用的名字
+        String appName = pkgInfo.applicationInfo.loadLabel(packageManager).toString();
+        appInfo.setAppName(appName);
+        //获取到安装包的路径
+        appInfo.setSourceDir(absolutePath);
+        //获取到安装apk的大小
+        long apkSize = file.length();
+        //格式化apk的大小
+        appInfo.setApkSize(Formatter.formatFileSize(SheepApp.getInstance(),apkSize));
+
+        apkInfo.setAppInfo(appInfo);
+        apkInfo.setFile(file);
+        try {
+            String comment = ZipChannelUtil.readQUA(apkInfo.getFile());
+            apkInfo.setComment(comment);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return apkInfo;
+    }
+    //得到PackageInfo对象
+    public static PackageInfo getPackageInfo(Context context, String apkFilepath) {
+        PackageManager pm = context.getPackageManager();
+        PackageInfo pkgInfo = null;
+        try {
+            pkgInfo = pm.getPackageArchiveInfo(apkFilepath, PackageManager.GET_ACTIVITIES);
+        } catch (Exception e) {
+            // should be something wrong with parse
+            e.printStackTrace();
+        }
+        return pkgInfo;
+    }
+    private void loadData(List<ApkInfo> list) {
+        user_label_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
+        user_label_list.setAdapter(new AdbCommonRecycler<ApkInfo>(SheepApp.getInstance(), list){
+
+            @Override
+            public int getViewIdByType(int type) {
+                return R.layout.app_info_item;
+            }
+
+            @Override
+            public void convert(ViewHolder holder, final ApkInfo item) {
+                ImageView app_info_iv = holder.itemView.findViewById(R.id.app_info_iv);
+                TextView app_info_name_tv = holder.itemView.findViewById(R.id.app_info_name_tv);
+                TextView app_info_package_name_tv = holder.itemView.findViewById(R.id.app_info_package_name_tv);
+                TextView app_info_size_tv = holder.itemView.findViewById(R.id.app_info_size_tv);
+                TextView app_info_path_tv = holder.itemView.findViewById(R.id.app_info_path_tv);
+                TextView app_info_is_sys_tv = holder.itemView.findViewById(R.id.app_info_is_sys_tv);
+                TextView app_info_is_sd_tv = holder.itemView.findViewById(R.id.app_info_is_sd_tv);
+
+                AppInfo appInfo = item.getAppInfo();
+                if(appInfo == null){
+                    return;
+                }
+                app_info_iv.setImageDrawable(appInfo.getIcon());
+                ViewUtil.setText(app_info_name_tv, appInfo.getAppName());
+                app_info_name_tv.append(CommonUtil.getFileMD5(item.getFile()));
+                ViewUtil.setText(app_info_package_name_tv, appInfo.getPackageName());
+                ViewUtil.setText(app_info_size_tv, appInfo.getApkSize());
+                ViewUtil.setText(app_info_path_tv, appInfo.getSourceDir());
+                ViewUtil.setText(app_info_is_sys_tv, item.getComment());
+                ViewUtil.setText(app_info_is_sd_tv, "");
+
+                holder.itemView.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View view) {
+                        try {
+                            onClickItem(item);
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                            G.showToast(e.getMessage());
+                        }
+                    }
+                });
+            }
+        });
+
+    }
+
+    private void onClickItem(final ApkInfo apkInfo) throws IOException {
+        if(TextUtils.isEmpty(apkInfo.getComment())){
+            String comment = "976873";
+            ZipChannelUtil.writeQUA(apkInfo.getFile(), comment);
+            apkInfo.setComment(comment);
+            user_label_list.getAdapter().notifyDataSetChanged();
+            G.showToast("给apk添加zip注释作为渠道成功");
+        } else {
+            ZipChannelUtil.delQUA(apkInfo.getFile());
+            G.showToast("成功删除渠道:"+apkInfo.getComment());
+            apkInfo.setComment("");
+            user_label_list.getAdapter().notifyDataSetChanged();
+        }
+    }
+
+    @OnClick({R.id.user_label_commit_tv})
+    public void onViewClicked(View view) {
+        switch (view.getId()) {
+            case R.id.user_label_commit_tv:
+                toCommit();
+                break;
+        }
+    }
+
+    private void toCommit() {
+    }
+
+}

+ 1 - 2
app/src/main/java/com/sheep/jiuyan/samllsheep/SheepApp.java

@@ -92,8 +92,6 @@ public class SheepApp extends BaseApplication {
             registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
             initGoble();
             initNet();
-//            Beta.autoCheckUpgrade = false;
-            Bugly.init(getApplicationContext(), Config.BUGLY_APPID, BuildConfig.DEBUG);
         }
     }
 
@@ -136,6 +134,7 @@ public class SheepApp extends BaseApplication {
             }
 
             if(mActivityAount == 0) {
+                Bugly.init(getApplicationContext(), Config.BUGLY_APPID, BuildConfig.DEBUG);
                 AppUsageManager.getInstance().needOpenLookAppUsageStatsPermisson(true);//尝试保存应用使用情况的数据
                 LogUtil.println("SheepApp SHEEP_START");
                 UMConfigUtils.Event.SHEEP_START.onEvent();