Возможность инициализации ViewActivity по сохранённому состоянию

This commit is contained in:
Victor 2015-04-23 14:21:36 +03:00
parent a8bb640f91
commit aeb74635f9

View File

@ -1,11 +1,13 @@
package com.annimon.everlastingsummer; package com.annimon.everlastingsummer;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.annimon.everlastingsummer.TouchGesture.TouchGestureType; import com.annimon.everlastingsummer.TouchGesture.TouchGestureType;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.media.MediaPlayer; import android.media.MediaPlayer;
@ -30,6 +32,8 @@ import android.widget.Toast;
public final class ViewActivity extends Activity implements TouchGesture.OnTouchGestureListener { public final class ViewActivity extends Activity implements TouchGesture.OnTouchGestureListener {
public static final String EXTRA_NAME = "name"; public static final String EXTRA_NAME = "name";
public static final String EXTRA_SAVE = "save";
private static final FadeInfo NO_FADE = new FadeInfo(false, false, 0); private static final FadeInfo NO_FADE = new FadeInfo(false, false, 0);
private static ViewActivity instance; private static ViewActivity instance;
@ -37,6 +41,8 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
return instance; return instance;
} }
private String scriptPath;
private ImageView background; private ImageView background;
private FrameLayout container; private FrameLayout container;
private TextView textview; private TextView textview;
@ -72,17 +78,17 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
characters = new CharactersES(); characters = new CharactersES();
characters.makeNamesKnown(); characters.makeNamesKnown();
String scriptpath = "";
final Intent intent = getIntent(); final Intent intent = getIntent();
final SaveInfo save = intent.getParcelableExtra(EXTRA_SAVE);
try { try {
InputStream stream = null; InputStream stream = null;
if (intent.hasExtra(EXTRA_NAME)) { if (save != null) {
scriptpath = PathResolver.script( intent.getStringExtra(EXTRA_NAME) ); stream = fromSave(save);
stream = getAssets().open(scriptpath); Toast.makeText(this, "Загружено", Toast.LENGTH_SHORT).show();
} else if (intent.hasExtra(EXTRA_NAME)) {
stream = fromAssets( PathResolver.script(intent.getStringExtra(EXTRA_NAME)) );
} else if (intent.getData() != null) { } else if (intent.getData() != null) {
final Uri fileUri = intent.getData(); stream = fromFile(intent.getData());
scriptpath = fileUri.getPath();
stream = getContentResolver().openInputStream(fileUri);
} else { } else {
Toast.makeText(this, "Нет данных для отображения", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Нет данных для отображения", Toast.LENGTH_LONG).show();
finish(); finish();
@ -92,13 +98,30 @@ public final class ViewActivity extends Activity implements TouchGesture.OnTouch
Parser.getInstance().next(); Parser.getInstance().next();
stream = null; stream = null;
} catch (Exception ex) { } catch (Exception ex) {
final String message = "Ошибка при открытии файла " + scriptpath; final String message = "Ошибка при открытии файла " + scriptPath;
if (Logger.DEBUG) Logger.log(message, ex); if (Logger.DEBUG) Logger.log(message, ex);
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); Toast.makeText(this, message, Toast.LENGTH_LONG).show();
finish(); finish();
} }
} }
private InputStream fromAssets(String path) throws IOException {
scriptPath = path;
return getAssets().open(scriptPath);
}
private InputStream fromFile(Uri fileUri) throws IOException {
scriptPath = fileUri.toString();
return getContentResolver().openInputStream(fileUri);
}
private InputStream fromSave(SaveInfo save) throws IOException {
if (save.getPath().startsWith(ContentResolver.SCHEME_FILE)) {
return fromFile(Uri.parse(save.getPath()));
}
return fromAssets(save.getPath());
}
@Override @Override
protected void onPause() { protected void onPause() {
stopMusic(NO_FADE); stopMusic(NO_FADE);