android146 360 病毒查杀

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="病毒查杀"
        android:textSize="24sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >
       <!--  帧布局是一层层往上盖,所以就是图片的叠加,后添加的在上面,先添加的在下面 -->
        <FrameLayout        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_scanner_malware" />
            <ImageView
                android:id="@+id/iv_scanning"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/act_scanning_03" />
        </FrameLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/tv_init_virus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="初始化8核杀毒引擎" />
            <ProgressBar
                android:id="@+id/progressBar1"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />
        </LinearLayout>
    </LinearLayout>
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
</LinearLayout>
package com.itheima.mobileguard.activities;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.util.List;

import org.w3c.dom.Text;

import com.itheima.mobileguard.R;
import com.itheima.mobileguard.db.dao.AntivirusDao;
import com.itheima.mobileguard.domain.AppInfo;
import com.itheima.mobileguard.engine.AppInfoParser;
import com.itheima.mobileguard.utils.Md5Utils;

import android.R.bool;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;

public class AntivirusActivity extends Activity {
    // 扫描开始
    protected static final int BEGING = 1;
    // 扫描中
    protected static final int SCANING = 2;
    // 扫描结束
    protected static final int FINISH = 3;
    private Message message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();
        initData();
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case BEGING:
                tv_init_virus.setText("初始化八核引擎");
                break;
            case SCANING:
                // 病毒扫描中:
                TextView child = new TextView(AntivirusActivity.this);
                ScanInfo scanInfo = (ScanInfo) msg.obj;
                // 如果为true表示有病毒
                if (scanInfo.desc) {
                    child.setTextColor(Color.RED);
                    child.setText(scanInfo.appName + "有病毒");
                } else {
                    child.setTextColor(Color.BLACK);
//                    // 为false表示没有病毒
                    child.setText(scanInfo.appName + "扫描安全");
                }

                ll_content.addView(child,0);
                //自动滚动
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        //一直往下面进行滚动
                        scrollView.fullScroll(scrollView.FOCUS_DOWN);
                    }
                });
                System.out.println(scanInfo.appName + "扫描安全");
                break;
            case FINISH:
                // 当扫描结束的时候。停止动画
                iv_scanning.clearAnimation();
                break;
            }
        };
    };
    
    private TextView tv_init_virus;
    private ProgressBar pb;
    private ImageView iv_scanning;
    private LinearLayout ll_content;
    private ScrollView scrollView;

    private void initData() {
        new Thread() {
            public void run() {
                message = Message.obtain();
                message.what = BEGING;

                PackageManager packageManager = getPackageManager();
                // 获取到所有安装的应用程序
                List<PackageInfo> installedPackages = packageManager
                        .getInstalledPackages(0);
                // 返回手机上面安装了多少个应用程序
                int size = installedPackages.size();
                // 设置进度条的最大值
                pb.setMax(size);
                int progress = 0;
                for (PackageInfo packageInfo : installedPackages) {
                    ScanInfo scanInfo = new ScanInfo();

                    // 获取到当前手机上面的app的名字
                    String appName = packageInfo.applicationInfo.loadLabel(
                            packageManager).toString();
                    scanInfo.appName = appName;
                    String packageName = packageInfo.applicationInfo.packageName;
                    scanInfo.packageName = packageName;
                    
                    // 首先需要获取到每个应用程序的目录
                    String sourceDir = packageInfo.applicationInfo.sourceDir;
                    // 获取到文件的md5
                    String md5 = Md5Utils.getFileMd5(sourceDir);
                    /*public static String getFileMd5(String sourceDir) {
                        File file = new File(sourceDir);
                        try {
                            FileInputStream fis = new FileInputStream(file);
                            byte[] buffer =  new byte[1024];
                            int len = -1;
                            //获取到数字摘要
                            MessageDigest messageDigest = MessageDigest.getInstance("md5");
                            while((len = fis.read(buffer))!= -1){
                                messageDigest.update(buffer, 0, len);
                            }
                            byte[] result = messageDigest.digest();
                            StringBuffer sb = new StringBuffer();
                            for(byte b : result){
                                int number = b&0xff; // 加盐 +1 ;
                                String hex = Integer.toHexString(number);
                                if(hex.length()==1){
                                    sb.append("0"+hex);
                                }else{
                                    sb.append(hex);
                                }
                            }
                            return sb.toString();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }*/
                    // 判断当前的文件是否是病毒数据库里面(根据应用的md5判断是否在病毒库中)
                    String desc = AntivirusDao.checkFileVirus(md5);
                    System.out.println("-------------------------");
                    System.out.println(appName);
                    System.out.println(md5);
                    
//                    03-06 07:37:32.505: I/System.out(23660): 垃圾
//                    03-06 07:37:32.505: I/System.out(23660): 51dc6ba54cbfbcff299eb72e79e03668
//                    ["md5":"51dc6ba54cbfbcff299eb72e79e03668","desc":"蝗虫病毒赶快卸载","desc":"蝗虫病毒赶快卸载","desc":"蝗虫病毒赶快卸载"]
//                    B7DA3864FD19C0B2390C9719E812E649
                    // 如果当前的描述信息等于null说明没有病毒
                    if (desc == null) {
                        scanInfo.desc = false;
                    } else {
                        scanInfo.desc = true;
                    }
                    progress++;
                    
                    SystemClock.sleep(100);
                    pb.setProgress(progress);
                    message = Message.obtain();
                    message.what = SCANING;
                    message.obj = scanInfo;
                    handler.sendMessage(message);
                }

                message = Message.obtain();
                message.what = FINISH;
                handler.sendMessage(message);
            };
        }.start();

    }

    static class ScanInfo {
        boolean desc;
        String appName;
        String packageName;
    }

    private void initUI() {
        setContentView(R.layout.activity_antivirusa);
        iv_scanning = (ImageView) findViewById(R.id.iv_scanning);
        tv_init_virus = (TextView) findViewById(R.id.tv_init_virus);
        pb = (ProgressBar) findViewById(R.id.progressBar1);
        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        scrollView = (ScrollView) findViewById(R.id.scrollView);

        //图片转起来
        /**
         * 第一个参数表示开始的角度 ,第二个参数表示结束的角度,
         *  第三个参数表示X轴参照自己从0.5开始,第四个参数表示Y轴参照自己从0.5开始,
         *  初始化旋转动画
         */
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        // 设置动画的时间
        rotateAnimation.setDuration(5000);
        // 设置动画无限循环
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        // 开始动画
        iv_scanning.startAnimation(rotateAnimation);
    }
}
package com.itheima.mobileguard.db.dao;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.itheima.mobileguard.domain.Virus;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//主动防御:如果有软件想改变c盘改变浏览器改变注册表就认为是病毒,权限判断就是提醒用户他会改变这些东西。
public class AntivirusDao {
 
    /**
     * 检查当前的md5值是否在病毒数据库
     */
    public static String checkFileVirus(String md5){
        String desc = null;
        SQLiteDatabase db = SQLiteDatabase.openDatabase(
                "/data/data/com.itheima.mobileguard/files/antivirus.db", null,
                SQLiteDatabase.OPEN_READONLY);
        //查询当前传过来的md5是否在病毒数据库里面
        Cursor cursor = db.rawQuery("select desc from datable where md5 = ?", new String[]{md5});
        //判断当前的游标是否可以移动
        if(cursor.moveToNext()){
            desc = cursor.getString(0);
        }
        cursor.close();
        return desc;
    }
    /**
     * 添加病毒数据库
     * @param md5  特征码
     * @param desc 描述信息
     */
    public static void addVirus(String md5,String desc){
        SQLiteDatabase db = SQLiteDatabase.openDatabase(
                "/data/data/com.itheima.mobileguard/files/antivirus.db", null,
                SQLiteDatabase.OPEN_READWRITE);
        ContentValues values = new ContentValues();
        values.put("md5", md5);
        values.put("type", 6);
        values.put("name", "Android.Troj.AirAD.a");
        values.put("desc", desc);
        db.insert("datable", null, values);
        db.close();
    }
    
    /**
     * 联网进行更新病毒数据库
     */
    private void updataVirus() {
        dao = new AntivirusDao();
        //联网从服务器获取到最新数据的md5的特征码
        HttpUtils httpUtils = new HttpUtils();
        
        String url = "http://192.168.13.126:8080/virus.json";
        httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                System.out.println(arg0.result);
//                {"md5":"51dc6ba54cbfbcff299eb72e79e03668","desc":"蝗虫病毒赶快卸载"}
                try {
                    JSONObject jsonObject = new JSONObject(arg0.result);
                    Gson gson = new Gson();
                    //解析json
                    Virus virus = gson.fromJson(arg0.result, Virus.class);
//                    String md5 = jsonObject.getString("md5");
//                    String desc = jsonObject.getString("desc");
                    dao.addVirus(virus.md5, virus.desc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
原文地址:https://www.cnblogs.com/yaowen/p/5154914.html