/* 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 java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.text.format.DateFormat; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RelativeLayout; import android.widget.SlidingDrawer; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import com.autburst.picture.gallery.GalleryImageAdapter; public class GalleryActivity extends Activity { private static final String TAG = GalleryActivity.class.getSimpleName(); private static final int DIALOG_UPLOAD_IN_PROGRESS = 1; private Gallery gallery; private ImageView galleryImageView; private GalleryImageAdapter adapter; //toolBox private RelativeLayout toolBar; private TextView picDate; private ImageButton deleteButton; private ImageButton clearButton; private ImageButton playButton; private ImageButton postButton; private ImageButton emailButton; private LinearLayout controlPanel; private String albumName; private ProgressDialog pd; //sliding drawer private SlidingDrawer drawer; private ImageView handle; private int uploadsInProgress; private SharedPreferences preferences; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery); uploadsInProgress = 0; preferences = getSharedPreferences(Utilities.PIC_STORE, 0); albumName = getIntent().getStringExtra("albumName"); //sliding drawer handle = (ImageView) findViewById(R.id.handle); drawer = (SlidingDrawer) findViewById(R.id.drawer); drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { // change background resource of handle to expanded handle.setBackgroundResource(R.drawable.bottom_switcher_expanded_background); } }); drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { @Override public void onDrawerClosed() { // change background resource of handle to collapsed handle.setBackgroundResource(R.drawable.bottom_switcher_collapsed_background); } }); //set and add listener frame rate radiogroup final String preferenceId = albumName + ".frameRate"; float frameRate = preferences.getFloat(preferenceId, Utilities.MEDIUM_FRAME_RATE); if(frameRate == Utilities.SLOW_FRAME_RATE) ((RadioButton) findViewById(R.id.slow)).setChecked(true); else if(frameRate == Utilities.MEDIUM_FRAME_RATE) ((RadioButton) findViewById(R.id.medium)).setChecked(true); else if(frameRate == Utilities.FAST_FRAME_RATE) ((RadioButton) findViewById(R.id.fast)).setChecked(true); RadioGroup frameRateRadioGroup = (RadioGroup) findViewById(R.id.frameRateGroup); frameRateRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int checkedId) { Editor edit = preferences.edit(); if(checkedId == R.id.slow) edit.putFloat(preferenceId, Utilities.SLOW_FRAME_RATE); else if(checkedId == R.id.medium) edit.putFloat(preferenceId, Utilities.MEDIUM_FRAME_RATE); else if(checkedId == R.id.fast) edit.putFloat(preferenceId, Utilities.FAST_FRAME_RATE); edit.commit(); } }); //gallery view gallery = (Gallery) findViewById(R.id.gallery); galleryImageView = (ImageView) findViewById(R.id.galleryImageView); toolBar = (RelativeLayout) findViewById(R.id.toolBar); picDate = (TextView) findViewById(R.id.date); deleteButton = (ImageButton) findViewById(R.id.deletePic); clearButton = (ImageButton) findViewById(R.id.clearPic); controlPanel = (LinearLayout) findViewById(R.id.controlPanel); postButton = (ImageButton) findViewById(R.id.postButton); postButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { startUpload(); } }); emailButton = (ImageButton) findViewById(R.id.emailButton); emailButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { startUpload(); } }); playButton = (ImageButton) findViewById(R.id.play_button); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(GalleryActivity.this, PlayMovieActivity.class); intent.putExtra("albumName", albumName); startActivity(intent); } }); enablePlayButton(); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.d(TAG, "You have selected picture: " + arg2 + " AlbumName: " + albumName); disablePlayButton(); File albumDirectory = Utilities.getAlbumDirectory(albumName); File[] listOfFiles = albumDirectory.listFiles(); Arrays.sort(listOfFiles); final File selectedPic = listOfFiles[arg2]; Date date = new Date(selectedPic.lastModified()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inSampleSize = 8; // File file = new File(, Utilities.g[arg2]); Bitmap bm = Bitmap.createBitmap(BitmapFactory.decodeFile(selectedPic.getAbsolutePath(), options)); galleryImageView.setImageBitmap(bm); // Toast.makeText(getBaseContext(), DateFormat.format("dd.MM.yyyy", date), // Toast.LENGTH_LONG).show(); picDate.setText(DateFormat.format("dd.MM.yyyy kk:mm", date)); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Log.d(TAG, "deleteButton.onClick()"); boolean isOK = true; synchronized (this) { Log.d(TAG, "uploadsInProgress: " + uploadsInProgress); if (uploadsInProgress != 0) { isOK = false; } } if (! isOK) { showDialog(GalleryActivity.DIALOG_UPLOAD_IN_PROGRESS); return; } Log.d(TAG, "Delete image file " + selectedPic.getAbsolutePath()); selectedPic.delete(); galleryImageView.setImageBitmap(null); toolBar.setVisibility(View.GONE); adapter.notifyDataSetChanged(); enablePlayButton(); } }); clearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Log.d(TAG, "clear view" ); galleryImageView.setImageBitmap(null); toolBar.setVisibility(View.GONE); enablePlayButton(); } }); toolBar.setVisibility(View.VISIBLE); } }); adapter = new GalleryImageAdapter(this, Utilities.getAlbumDirectory(albumName)); gallery.setAdapter(adapter); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { synchronized (GalleryActivity.this) { uploadsInProgress--; } Intent i = new Intent(GalleryActivity.this, FinishedUploadActivity.class); i.putExtra(UploadService.ALBUM_NAME, arg1.getStringExtra(UploadService.ALBUM_NAME)); GalleryActivity.this.startActivity(i); } }; protected void onResume() { super.onResume(); registerReceiver(receiver, new IntentFilter(FinishedUploadActivity.UPLOAD_FINISHED)); }; protected void onPause() { super.onPause(); unregisterReceiver(receiver); }; private void startUpload() { /* pd = ProgressDialog.show(GalleryActivity.this, "Working..", "Uploading images to server", true, false); */ synchronized (this) { uploadsInProgress++; Log.d(TAG, "uploadsInProgress: " + uploadsInProgress); } Log.d(TAG, "start thread"); //Toast.makeText(this, "Upload started in background!", Toast.LENGTH_LONG); Intent i = new Intent(this, UploadService.class); i.putExtra(UploadService.ALBUM_NAME, albumName); startService(i); } protected Dialog onCreateDialog(int id) { final Dialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this); switch(id) { case DIALOG_UPLOAD_IN_PROGRESS: builder.setTitle(R.string.upload_in_progress); builder.setMessage(R.string.pictures_not_deleteable); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); dialog = builder.create(); break; default: dialog = null; } return dialog; } public void enablePlayButton() { controlPanel.setVisibility(View.VISIBLE); galleryImageView.setVisibility(View.GONE); } public void disablePlayButton() { controlPanel.setVisibility(View.GONE); galleryImageView.setVisibility(View.VISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { new MenuInflater(this).inflate(R.menu.gallerymenu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.playMovieMenuItem: Intent intent = new Intent(this, PlayMovieActivity.class); intent.putExtra("albumName", albumName); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } }