package com.jiuqi.njt.ui;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.jiuqi.mobile.nigo.comeclose.bean.base.FileUploadBean;
import com.jiuqi.mobile.nigo.comeclose.manager.base.IFileUploadManager;
import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext;
import com.jiuqi.njt.R;
import com.jiuqi.njt.adapter.ErrorLogSelectAdapter;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.update.Config;
import com.jiuqi.njt.update.UpdateVersion;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.DateUtil;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.widget.UpFileDialog;
import com.jiuqi.njt.widget.ProgressDialogStyle;
/**============================================================
* 版权: 久其软件 版权所有 (c)
* 包:
* 修改记录:
* 日期 作者 内容
* =============================================================
* 2013-6-13 liyue
* ============================================================*/
/**
* <p>
* 关于界面
* </p>
*
* <p>
* Copyright: 版权所有 (c)<br>
* Company: 久其
* </p>
*
* @author liyue
* @version 2013-6-13
*/
public class AboutActivity extends BaseActivity implements OnClickListener {
private View btnWebSite;
private View btnUpdate;
private View btnHotLine;
private View btnUploadErrorLog;
private View btnTutorial;
private View btnFeedBack;
private String strWebsite;
private String strHotline;
private TextView tvVersion;
private MyApp application;
private OptsharepreInterface sharePre;
private byte[] fileData;
private String path = "";
private String helpUrl;
private ErrorLogSelectAdapter errorAdapter;
private ArrayList<String> fileList;
private final int startIndex = 6;
private final int endIndex = 16;
private ArrayList<String> upFileList;
private String[] fileArrays;
private AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
strWebsite = getResources().getString(R.string.website);
strHotline = getResources().getString(R.string.hotlineText);
helpUrl = Constants.APK_HELP_URL;
application = (MyApp) getApplicationContext();
sharePre = new OptsharepreInterface(AboutActivity.this);
}
private void initWidgets() {
setContentView(R.layout.about_activity);
View rlTitleBar = findViewById(R.id.titleBarStub);
TitleBarUtil.createTitleBar(this, rlTitleBar, "关于");
tvVersion = (TextView) findViewById(R.id.tvVersion);
btnWebSite = findViewById(R.id.btnWebSite);
btnHotLine = findViewById(R.id.btnHotLine);
btnUpdate = findViewById(R.id.btnUpdate);
btnUploadErrorLog = findViewById(R.id.btnUploadErrorLog);
btnTutorial = findViewById(R.id.btnTutorial);
btnFeedBack = findViewById(R.id.btnFeedback);
}
private void initListeners() {
btnWebSite.setOnClickListener(this);
btnHotLine.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnUploadErrorLog.setOnClickListener(this);
btnTutorial.setOnClickListener(this);
btnFeedBack.setOnClickListener(this);
}
private void initUI() {
tvVersion.setText(Config.getVerName(this).replace("ver", "版本号").replace("build", "日期"));
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.btnUpdate:
UpdateVersion uv = new UpdateVersion();
uv.checkVersion(AboutActivity.this, true);
break;
case R.id.btnWebSite:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strWebsite));
startActivity(intent);
break;
case R.id.btnHotLine:
strHotline = strHotline.replace("-", "");
UIUtil.tryToDial(this, strHotline, "尊敬的用户,正在为您接通农机通客服热线:", ",是否继续?");
break;
case R.id.btnUploadErrorLog:
path = DateUtil.getLogPath(AboutActivity.this);
ArrayList<String> dataList = getShowDataSize(7);// 最大显示的天数
if(null!=dataList&&dataList.size()>0){
showSelectDialog(dataList); //
}
break;
case R.id.btnTutorial:
intent = new Intent();
intent.setClass(this, NxwModulerActivity.class);
intent.putExtra(Constants.PARAM_URL, helpUrl);
startActivity(intent);
break;
case R.id.btnFeedback:
intent = new Intent(AboutActivity.this, FeedBackActivity.class);
startActivity(intent);
break;
default:
break;
}
}
private ArrayList<String> getShowDataSize(int wantShowDays) {
ArrayList<String> dateList = new ArrayList<String>();
fileList =getFiles(path);
try {
if (null == fileList || fileList.size() == 0) {
UIUtil.showMsg(AboutActivity.this, "没有错误数据");
} else {
fileArrays = new String[fileList.size()];
for (int i = 0; i < fileList.size(); i++) {
if(fileList.get(i).length()>21){
try {
File file = new File(path +File.separator+fileList.get(i));
file.delete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
fileArrays[i] = fileList.get(i);
}
Arrays.sort(fileArrays);
for (String string : fileArrays) {
dateList.add(string.substring(startIndex, endIndex));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dateList;
}
@SuppressWarnings("rawtypes")
private void showSelectDialog(ArrayList<String> list) {
upFileList = new ArrayList<String>();
final UpFileDialog mdialog = new UpFileDialog(AboutActivity.this);
alert = new AlertDialog.Builder(AboutActivity.this)
.create();
alert.setView(mdialog.getView(), -1, -1, -1, -1);
errorAdapter = new ErrorLogSelectAdapter(AboutActivity.this, list);
mdialog.getDateListView().setAdapter(errorAdapter);
mdialog.getDateListView().setOnItemClickListener(
new DialogListItemListener(null, list, alert));
alert.show();
mdialog.getUpload().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HashSet<String> items = errorAdapter.getSelectedItems();
if (items != null && items.size() > 0) {
for (Iterator it = items.iterator(); it.hasNext();) {
try {
String selectItem = it.next().toString();
upFileList.add(selectItem);
} catch (Exception e) {
e.printStackTrace();
}
}
if (upFileList.size() > 0) {
UploadLogTask uplogTask = new UploadLogTask();
uplogTask.execute();
}
}
}
});
}
class DialogListItemListener implements OnItemClickListener {
private List<String> list;
private AlertDialog alert;
private TextView v;
public DialogListItemListener(View v, List<String> list,
AlertDialog alert) {
this.list = list;
this.alert = alert;
this.v = (TextView) v;
}
@SuppressLint("SimpleDateFormat")
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String selectItem = list.get(position);
upFileList.add(selectItem);
if (upFileList.size() > 0) {
UploadLogTask uplogTask = new UploadLogTask();
uplogTask.execute();
}
alert.dismiss();
}
}
public static byte[] getBytes(InputStreamReader is) throws IOException {
StringBuffer sb = new StringBuffer();
// String encoding = "UTF-8";
BufferedReader bufferedReader = new BufferedReader(is);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
sb.append(lineTxt);
}
is.close();
return sb.toString().getBytes();
}
/**
* 日志上传
*
* @author malingya
*
*/
private class UploadLogTask extends AsyncTask<Void, Void, String> {
Dialog pd = null;
protected void onPreExecute() {
pd = ProgressDialogStyle.createLoadingDialog(AboutActivity.this,
null);
pd.show();
}
protected String doInBackground(Void... params) {
String msg = "";
try {
ClientContext context;
if (application.getIsLogin()) {
context = application.getClientContext();
} else {
context = ClientContext.getClientContext(
Constants.SERVER_URL, Constants.ANONYMOUS,
Constants.ANONYMOUS);
application.setClientContext(context);
}
IFileUploadManager manager = context
.getManager(IFileUploadManager.class);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imsi = tm.getSubscriberId();
for (String fileName : upFileList) {
FileUploadBean bean = new FileUploadBean();
bean.setCreateDate();
InputStreamReader isStream = new InputStreamReader(
new FileInputStream(new File(path + File.separator
+ "crash-" + fileName + ".log")), "UTF-8");// 考虑到编码格式
fileData = getBytes(isStream);
bean.setFileData(fileData);
bean.setFileName(fileName);
if (application.getIsLogin()) {
bean.setUserGuid(sharePre.getPres("guid"));
}
bean.setImei(imsi);
manager.create(bean);
// 如果上传成功就删除文件
deletefile(fileName);
}
msg = "上传成功";
} catch (Exception e) {
msg = "上传日志异常";
e.printStackTrace();
}
return msg;
}
protected void onCancelled() {
super.onCancelled();
}
protected void onPostExecute(String result) {
pd.dismiss();
if (!TextUtils.isEmpty(result.trim())) {
UIUtil.showMsg(AboutActivity.this, result);
if("上传成功".equals(result)){
alert.dismiss();
}
}
}
}
/**
* 循环遍历一个文件夹取出下面一个目录下的所有文件,并返回所有文件的文件名称
*
* @param filePath
* @return
*/
public ArrayList<String> getFiles(String filePath) {
ArrayList<String> filelist = new ArrayList<String>();
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File root = new File(filePath);
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
getFiles(file.getAbsolutePath());// 递归调用
Log.e("文件夹",
"显示" + filePath + "下所有子目录及其文件" + file.getAbsolutePath());
} else {
if(file.getName().length()>21){
try {
File deletefile = new File(path +File.separator+file.getName());
deletefile.delete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
filelist.add(file.getName());
Log.e("文件", "显示" + filePath + "下所有子目录" + file.getAbsolutePath());
}
}
}
return filelist;
}
/**
* 删除文件
*
* @author malingya
* @created 2013年8月20日
* @param
*/
private void deletefile(String fileName) {
try {
File file = new File(path + File.separator+"crash-" + fileName+".log");
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}