com.example.acer.readdata;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Message;
import android.util.Log;
/**
* Created by acer on 2015/11/11.
*/
public class ReadData extends BroadcastReceiver {
MainActivity execactivity;
private static final String TAG =
"UsbStateReceiver";
public static final int USB_STATE_MSG =
0x00020;
public static final int USB_STATE_ON =
0x00021;
public static final int USB_STATE_OFF =
0x00022;
public IntentFilter filter =
new IntentFilter();
public ReadData(Context context) {
execactivity = (MainActivity) context;
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_CHECKING);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme(
"file");
Log.v(
"msg",
"filter");
}
public Intent
registerReceiver(){
return execactivity.registerReceiver(
this,filter);
}
public void unregisterReceiver(){
execactivity.unregisterReceiver(
this);
}
@Override
public void onReceive(Context context, Intent intent) {
if(execactivity.mHandler ==
null){
Log.v(
"msg",
"null");
return;
}
Message msg =
new Message();
msg.what = USB_STATE_MSG;
if( intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED )){
msg.what = USB_STATE_ON;
Log.v(
"msg",
"msg.arg1 = USB_STATE_ON");
execactivity.mHandler.sendMessage(msg);
}
else{
msg.what = USB_STATE_OFF;
Log.v(
"msg",
"msg.arg1 = USB_STATE_OFF");
execactivity.mHandler.sendMessage(msg);
}
Log.v(
"msg",
"sendMessage");
}
}
**MyDatabastHelper.java**
package com.example.acer.readdata;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by acer on 2015/11/16.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String catalog_table =
"create table catalog("
+
"id integer primary key autoincrement,"
+
"type integer"
+
"name text"
+
"icon text"
+
"index integer)";
public static final String book_table =
"create table book("
+
"id integer primary key autoincrement,"
+
"parentid integer"
+
"catalogid integer"
+
"type integer"
+
"name text"
+
"icon text"
+
"url text"
+
"index integer"
+
"readcount integer"
+
"pos integer"
+
"readtime integer"
+
"reverse1 integer"
+
"reverse2 integer)";
public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,
int version){
super(context,name,factory,version);
Log.v(
"SQL",
" MyDatabaseHelper");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(catalog_table);
db.execSQL(book_table);
Log.v(
"SQL",
"onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {
db.execSQL(
"drop table if exists book_table");
db.execSQL(
"drop table if exists catalog_table");
Log.v(
"SQL",
"onUpgrade");
onCreate(db);
}
}
**MainActivity.java**
package com.example.acer.readdata;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Scanner;
import java.util.logging.LogRecord;
import static android.widget.Toast.*;
public class MainActivity extends Activity {
MyHandler mHandler;
MyThread thread;
ProgressDialog progressDialog;
private ReadData usbstates;
private MyDatabaseHelper dbHelper;
private int i =
2;
private int type;
private int csvline =
2;
private int nShowProgress =
0x1000;
private int nHideProgress =
0x1001;
private int nStepProgress =
0x1002;
private int nMutiProgress =
0x1003;
private int toast =
0x1004;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
dbHelper =
new MyDatabaseHelper(
this,
"book.db",
null,
5);
Log.v(
"msg2",
"dbHelper");
mHandler =
new MyHandler();
thread =
new MyThread();
usbstates =
new ReadData(
this);
progressDialog =
new ProgressDialog(MainActivity.
this);
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
protected void onStart() {
super.onStart();
usbstates.registerReceiver();
}
protected void onStop() {
super.onStop();
usbstates.unregisterReceiver();
}
class MyHandler extends Handler {
public MyHandler() {
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.v(
"msg",
"ainactivity handler");
if (msg.what ==
0x0021) {
Log.v(
"msg",
"Main.SB_STATE_ON");
thread.start();
Log.v(
"msg",
"Main.thread.start");
}
else if (msg.what ==
0x0022) {
Log.v(
"msg",
"Main SB_STATE_OFF");
}
else if (msg.what ==
8888) {
useChoice();
Log.v(
"msg",
"useChoice");
}
else if(msg.what == nShowProgress)
{
progressDialog.setTitle(
"更新数据中");
int nMaxProgress = msg.arg2;
Log.v(
"bar",
"msg.what =" + nShowProgress +
"msg.arg2 =" + nMaxProgress );
progressDialog.setMax(nMaxProgress);
progressDialog.show();
}
else if(msg.what == nHideProgress)
{
Log.v(
"bar",
"msg.what =" + nShowProgress);
progressDialog.hide();
}
else if(msg.what == nStepProgress) {
int iStep = msg.arg2;
Log.v(
"bar",
"msg.what =" + nShowProgress +
"" + iStep);
progressDialog.setProgress(iStep);
}
else if(msg.what == nMutiProgress)
{
int nMax = msg.arg1;
int iStep = msg.arg2;
progressDialog.setMax(nMax);
Log.v(
"bar",
"msg.what =" + nShowProgress +
"" + iStep);
progressDialog.setProgress(iStep);
}
else if(msg.what == toast) {
Toast.makeText(MainActivity.
this,
"更新成功", Toast.LENGTH_SHORT).show();
Log.v(
"copy ",
"弹出更新成功");
}
}
}
class MyThread extends Thread {
public MyThread() {}
@Override
public void run() {
super.run();
Log.v(
"msg1",
"Main.MyThread");
File file =
new File(
"/storage/external_storage/sda1/HappyChildren_update/update.ini");
if (file ==
null) {
Log.v(
"msg1",
"file is null");
}
else
Log.v(
"msg1",
"file is not null");
try {
Log.v(
"msg1",
"try");
FileReader in =
new FileReader(file);
Log.v(
"msg1",
"in" + in);
char byt[] =
new char[
1024];
String substr =
"update=YES";
int len = in.read(byt);
Log.v(
"msg1",
"in.read:" + len);
String text =
new String(byt,
0, len);
Log.v(
"msg1",
"text:" + text);
if (text.equals(substr)) {
Log.v(
"msg",
"eqyals");
Message msg =
new Message();
msg.what =
8888;
mHandler.sendMessage(msg);
Log.v(
"msg",
"用户选择更新数据");
}
else {
Log.v(
"msg",
"else");
}
in.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void useChoice() {
AlertDialog.Builder builder =
new AlertDialog.Builder(MainActivity.
this);
builder.setTitle(
"更新数据");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage(
"是否更新数据?");
Log.v(
"msg2",
"useChoice is use");
builder.setPositiveButton(
"更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
Log.v(
"msg2",
"useChoice is onClick yes");
dowithFile();
}
});
builder.setNegativeButton(
"取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
builder.show();
}
private void dowithFile()
{
Thread th =
new Thread(
new Runnable() {
@Override
public void run() {
File file =
new File(
"/storage/external_storage/sda1/HappyChildren_update/update.csv");
if (file.exists()) {
Log.v(
"msg2",
"useChoice is exosts");
try {
Log.v(
"msg2",
"useChoice is try");
BufferedReader reader =
new BufferedReader(
new FileReader(
"/storage/external_storage/sda1/HappyChildren_update/update.csv"));
reader.readLine();
String line =
null;
Log.v(
"msg2",
"useChoice is tryover");
reader.mark((
int) file.length() +
1);
while ((line = reader.readLine()) !=
null) {
csvline++;
Log.v(
"csv",
"csvline: " + csvline);
}
Message msg = mHandler.obtainMessage();
msg.what = nShowProgress;
msg.arg2 = csvline;
mHandler.sendMessage(msg);
Log.v(
"copy",
"显示进度条");
reader.reset();
while ((line = reader.readLine()) !=
null) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
"/storage/emulated/0/HappyChildren1/db/book.db",
null);
String sType = csv(i,
3);
Log.v(
"csv",
"sType: " + sType);
if (sType.equals(
"绘本教学") ==
true) {
type =
0;
Log.v(
"type",
"sType type = 0");
}
else if (sType.equals(
"视频教学") ==
true) {
type =
1;
Log.v(
"type",
"sType type = 1");
}
else if (sType.equals(
"游戏娱乐") ==
true) {
type =
2;
Log.v(
"type",
"sType type = 2");
}
Cursor catalogtable = db.rawQuery(
"select * from catalog_table where type = ? and name like ?",
new String[]{String.valueOf(type), csv(i,
4)});
int count = catalogtable.getCount();
Log.v(
"type",
"catalogtable : " + count);
if (count ==
0) {
ContentValues values2 =
new ContentValues();
Log.v(
"type",
"values2: " + values2);
values2.put(
"type", type);
values2.put(
"name", csv(i,
4));
long catalogId = db.insert(
"catalog_table",
null, values2);
typeSort(catalogId, type);
}
else {
Cursor cursor = db.rawQuery(
"select * from catalog_table where type = ? and name = ?",
new String[]{String.valueOf(type), csv(i,
4)});
cursor.moveToFirst();
int catalogId = cursor.getInt(cursor.getColumnIndex(
"_id"));
Log.v(
"type",
"catalogId : " + catalogId);
typeSort(catalogId, type);
}
++i;
Message msg1 = mHandler.obtainMessage();
msg1.what = nStepProgress;
msg1.arg2 = i;
mHandler.sendMessage(msg1);
}
String oldPath =
"/storage/external_storage/sda1/HappyChildren_update/books";
String newPath =
"/storage/emulated/0/HappyChildren1/books";
Log.v(
"copy",
"oldpath: " + oldPath +
" newpath:" + newPath);
copyFolder(oldPath, newPath);
Log.v(
"copy",
"copyFolder");
Message msg3 = mHandler.obtainMessage();
msg3.what = nHideProgress;
mHandler.sendMessage(msg3);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
else {
Toast.makeText(MainActivity.
this,
"U盘缺少csv文件,无法更新", LENGTH_SHORT).show();
}
iniFile();
Message msg1 = mHandler.obtainMessage();
msg1.what = toast;
mHandler.sendMessage(msg1);
Log.v(
"copy",
"提示用户更新完成");
}
});
th.start();
}
public void typeSort(
long catalogId,
int parentid) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
"/storage/emulated/0/HappyChildren1/db/book.db",
null);
Cursor booktable = db.rawQuery(
"select * from book_table where catalogid = ? and name like ?",
new String[]{String.valueOf(catalogId), csv(i,
1)});
int count = booktable.getCount();
Log.v(
"type",
"booktable : " + count);
if (count ==
0) {
ContentValues values =
new ContentValues();
values.put(
"name", csv(i,
1));
values.put(
"icon", csv(i,
6));
values.put(
"reverse1", csv(i,
8));
values.put(
"parentid", String.valueOf(parentid));
values.put(
"type",
0);
values.put(
"catalogid",String.valueOf(catalogId));
values.put(
"url", csv(i,
7));
db.insert(
"book_table",
null, values);
}
else {
ContentValues values3 =
new ContentValues();
values3.put(
"url",csv(i,
7));
db.update(
"book_table", values3,
"name = ?",
new String[]{csv(i,
1)});
}
}
public String
csv(
int row,
int col) {
String sRet =
"";
Log.v(
"msg2",
"Csv");
try {
Log.v(
"msg2",
"CsvTry");
InputStream in =
new FileInputStream(
new File(
"/storage/external_storage/sda1/HappyChildren_update/update.csv"));
InputStreamReader input =
new InputStreamReader(in,
"gb2312");
BufferedReader reader =
new BufferedReader(input);
Log.v(
"msg2",
"CsvReader");
String line =
null;
int index =
0;
while ((line = reader.readLine()) !=
null) {
Log.v(
"msg2",
"CsvWhile");
if (index == row -
1) {
String item[] = line.split(
",");
Log.v(
"msg2",
"CsvIf");
if (item.length >= col -
1) {
Log.v(
"msg2",
"CsvIFif");
String last = item[col -
1];
sRet = last;
Log.v(
"msg2",
"last: " + last);
}
break;
}
index++;
Log.v(
"msg2",
"index: " + index);
}
}
catch (Exception e) {
e.printStackTrace();
Log.v(
"msg2",
"exception");
}
Log.v(
"csv",
"sRet = " + sRet);
return sRet;
}
public void copyFolder(String oldPath, String newPath) {
try {
(
new File(newPath)).mkdirs();
File f =
new File(oldPath);
Log.v(
"copy",
"f: " + f);
String[] file = f.list();
int nFile = (
int) file.length;
Message msg = mHandler.obtainMessage();
msg.what = nMutiProgress;
msg.arg1 = nFile;
msg.arg2 =
0;
mHandler.sendMessage(msg);
Log.v(
"copy",
"显示进度条");
File temp =
null;
for (
int i =
0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp =
new File(oldPath + file[i]);
Log.v(
"copy",
"temp: " + temp);
}
else {
temp =
new File(oldPath + File.separator + file[i]);
Log.v(
"copy",
"temp: " + temp);
}
if (temp.isFile()) {
FileInputStream input =
new FileInputStream(temp);
FileOutputStream output =
new FileOutputStream(newPath +
"/" + (temp.getName()).toString());
byte[] b =
new byte[
1024 *
1024];
int len;
File newfile =
new File(newPath);
while ((len = input.read(b)) != -
1) {
Log.v(
"copy",
"len:" + len);
output.write(b,
0, len);
Log.v(
"copy",
"len: " + len);
Message msg1 = mHandler.obtainMessage();
msg1.what = nMutiProgress;
msg1.arg1 = nFile;
msg1.arg2 = i+
1;
mHandler.sendMessage(msg1);
Log.v(
"copy",
"sendMessage(msg1)");
}
Log.v(
"copy",
"while over");
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {
copyFolder(oldPath +
"/" + file[i], newPath +
"/" + file[i]);
Log.v(
"copy",
"子文件夹");
}
}
}
catch (Exception e) {
System.out.println(
"复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
public void iniFile() {
File file =
new File(
"/storage/external_storage/sda1/HappyChildren_update/update.ini");
Log.v(
"msg2",
"iniFile");
try {
FileInputStream in =
new FileInputStream(file);
byte byt[] =
new byte[
1024];
int len = in.read(byt);
Log.v(
"msg2",
"len:" + len);
FileOutputStream out =
new FileOutputStream(file);
byte by[] =
"update=NO".getBytes();
out.write(by);
out.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
插入U盘,自动复制数据库信息和相关文件
标签: