package com.pixtas.yogapowervinyasa.activity;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.pixtas.framework.YogaAppContext;
import com.pixtas.helpers.DialogHelper;
import com.pixtas.helpers.FileDownHelper;
import com.pixtas.helpers.HardwareHelper;
import com.pixtas.models.DatabaseAdapter;
import com.pixtas.models.option.DataBaseOption;
import com.pixtas.models.rest.RestAPI;
import com.pixtas.models.struts.FlashUrl;
import com.pixtas.yogapowervinyasa.Config;
import com.pixtas.yogapowervinyasa.R;
import com.pixtas.yogapowervinyasa.YogaApp;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Home extends Activity implements OnClickListener,OnItemSelectedListener,OnTouchListener{
private static final String TAG = "Home";
private ProgressDialog pd = null;
private TextView playTitle;
private ProgressBar playPB;
private ImageView pre,pause,next,yogaImg;
private MediaPlayer mp;
private Spinner chapters;
private String saveDir;
private String saveAudioTo;
private String[] saveImgTos = null;
private int chapter = 1;
private boolean isPlaying = false;
/*-------------弹出窗口控件-------------*/
//下载窗口
private Dialog dlg;
private TextView cancel;
private ProgressBar installPB;
//设置窗口
private RadioButton englishRadio,sanskritRadio,spanishRadio;
private Button sureBtn,cancelBtn;
private RadioGroup languageRadioGroup;
private int arg = 0;
/*------------------------------------------*/
/*访问远程音频文件的大小*/
private int audioSize = 0;//flash音频大小
private int allImageSize = 0;//flash所有照片大小
private int flashSize = 0;//flash大小(audioSize + allImamgeSize)
private int audioTime = 0;//audio时间
private boolean switchChapter = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
YogaAppContext.initApp((YogaApp)getApplication());
DataBaseOption.databaseAdapter = new DatabaseAdapter(YogaAppContext.getApp());
if(HardwareHelper.connectedNetwork(YogaAppContext.getApp())){
if(!RestAPI.checkApkUpgrade()){
Builder builder = DialogHelper.showAlertBuilder(Home.this, R.string.main_need_update, R.string.warning);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String url = "market://details?id=com.pixtas.yogatestaudio";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
finish();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
initCreate();
}
});
builder.show();
}else{
this.initCreate();
}
}else{
this.initCreate();
}
}
/*创建视图之前的准备工作*/
private void initCreate(){
if(!DataBaseOption.isInstall()){
if(this.checkNet()){
this.progressDlg(1);//没下载完
}
}else{
if(HardwareHelper.connectedNetwork(YogaAppContext.getApp())){
if(RestAPI.checkNewVersion()){
Builder builder = DialogHelper.showAlertBuilder(Home.this,R.string.update_msg,R.string.warning);
builder.setCancelable(true);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDlg(2);//检测到有更改再下载
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
initFlashMsg();
}
});
builder.show();
}else{
this.initFlashMsg();
}
}else{
this.initFlashMsg();
}
}
this.initComponents();
}
/*加载老版本的东西*/
private void initFlashMsg(){
this.progressDlg(0);
}
/*加载flash信息*/
private void loadFlashMsg(){
RestAPI.initFlashUrl();
audioSize = Config.audioSize;
allImageSize = Config.allImageSize;
flashSize = audioSize + allImageSize;
}
/*删除照片*/
private void delPhotos(){
for(int i = 1;i <= FlashUrl.chaptersNum;i ++){
String path = FileDownHelper.getSaveImgName(YogaAppContext.getApp(),Integer.toString(i));
File f = new File(path);
if(f.exists()){
f.delete();
}
}
}
/*创建加载窗口*/
private void progressDlg(final int type){
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Loading...");
pd.setCancelable(false);
pd.show();
new Thread(){
public void run(){
if(type == 0){
RestAPI.initFlash();
}else if(type == 2){//检查更新
DataBaseOption.updateInstallVersion(1, Config.newVersion);
DataBaseOption.updateInstallData(1, 0);
delPhotos();
loadFlashMsg();
}else if(type == 3){//重新下载
DataBaseOption.updateInstallData(1, 0);
File audioFile = new File(saveAudioTo);
if(audioFile.exists()){
audioFile.delete();
}
delPhotos();
loadFlashMsg();
}else{
loadFlashMsg();
}
pd.dismiss();
}
}.start();
pd.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
if(type == 3){
playPB.setProgress(0);
pause.setImageResource(R.drawable.play);
}
initTitleSpinner();
initInstallAudioDlg(type);
}
});
}
private void initTitleSpinner(){
List<String> data = new ArrayList<String>();
switch (arg) {
case 0:
for(int i = 0 ;i < FlashUrl.photoEnglishTitles.length;i ++){
data.add(Integer.toString(i + 1) + ". " + FlashUrl.photoEnglishTitles[i]);
}
break;
case 1:
for(int i = 0 ;i < FlashUrl.photoSanskritTitles.length;i ++){
data.add(Integer.toString(i + 1) + ". " + FlashUrl.photoSanskritTitles[i]);
}
break;
case 2:
for(int i = 0 ;i < FlashUrl.photoSpanishTitles.length;i ++){
data.add(Integer.toString(i + 1) + ". " + FlashUrl.photoSpanishTitles[i]);
}
break;
default:
break;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Home.this,android.R.layout.simple_spinner_item,data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
chapters.setAdapter(adapter);
}
private boolean checkNet(){
if(!HardwareHelper.connectedNetwork(YogaAppContext.getApp())){
Builder builder = DialogHelper.showAlertBuilder(Home.this,R.string.no_wifi,R.string.warning);
builder.setNeutralButton(R.string.close,