/*
ApicAday - Everyday.. is different, your mood, your life.
Copyright (c) 2010
Oliver Selinger <oliver.selinger@autburst.com>,
Michael Greifeneder <michael.greifeneder@autburst.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.autburst.picture;
import java.io.File;
import java.util.Arrays;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PlayMovieActivity extends Activity implements Runnable {
private static final String TAG = PlayMovieActivity.class.getSimpleName();
private String albumName;
private ImageView imageView;
private float frameRate;
private File album;
private boolean run = true;
private Bitmap bm = null;
// private MediaPlayer musicPlayer;
private Thread imagePlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.play_movie);
imageView = (ImageView) findViewById(R.id.movieImage);
albumName = getIntent().getStringExtra("albumName");
album = Utilities.getAlbumDirectory(albumName);
SharedPreferences preferences = getSharedPreferences(Utilities.PIC_STORE, 0);
frameRate = preferences.getFloat(albumName + ".frameRate", Utilities.MEDIUM_FRAME_RATE);
//get full screen mode
updateFullscreenStatus(true);
// musicPlayer = MediaPlayer.create(this, R.raw.test);
imagePlayer = new Thread(this);
imagePlayer.start();
// musicPlayer.start();
}
protected void onResume() {
super.onResume();
// musicPlayer.start();
this.start();
}
protected void onPause() {
// musicPlayer.pause();
imagePlayer.stop();
super.onPause();
}
protected void onStop() {
// musicPlayer.stop();
super.onStop();
}
protected void onDestroy() {
// musicPlayer.release();
super.onDestroy();
}
private void updateFullscreenStatus(boolean bUseFullscreen)
{
if(bUseFullscreen)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
((LinearLayout) findViewById(R.id.movieLayout)).requestLayout();
}
public void shutdown() {
this.run = false;
}
boolean pause = false;
public void start() {
this.pause = false;
}
public void pause() {
this.pause = true;
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
synchronized (bm) {
imageView.setImageBitmap(bm);
}
}
};
int counter = 0;
public void run() {
Float timeToWait = frameRate*1000;
final long speed = timeToWait.intValue();
Log.d(TAG, "time to wait for next pic: " + speed);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = 4;
File[] images = album.listFiles();
Arrays.sort(images);
while (run && counter < images.length) {
Log.d(TAG, "Play pic:" + images[counter].getAbsolutePath());
synchronized (this) {
bm = Bitmap.createBitmap(BitmapFactory
.decodeFile(images[counter].getAbsolutePath(), options));
}
handler.sendEmptyMessage(0);
Log.d(TAG, "Show next pic!");
// get next image
// TODO parallel in other thread
counter++;
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
Log.e(TAG, "Movie was interrupted!", e);
}
}
bm = null;
// musicPlayer.stop();
}
}