|
|
|
@ -21,9 +21,8 @@ package fr.mobdev.goblim.activity;
|
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
import android.support.v7.app.ActionBarActivity;
|
|
|
|
@ -33,20 +32,19 @@ import android.view.MenuItem;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.Spinner;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import fr.mobdev.goblim.Database;
|
|
|
|
|
import fr.mobdev.goblim.listener.NetworkListener;
|
|
|
|
|
import fr.mobdev.goblim.listener.NetworkAdapter;
|
|
|
|
|
import fr.mobdev.goblim.NetworkManager;
|
|
|
|
|
import fr.mobdev.goblim.objects.Img;
|
|
|
|
|
import fr.mobdev.goblim.objects.Server;
|
|
|
|
@ -60,13 +58,11 @@ import fr.mobdev.goblim.R;
|
|
|
|
|
*/
|
|
|
|
|
public class MainActivity extends ActionBarActivity {
|
|
|
|
|
|
|
|
|
|
private NetworkListener listener;
|
|
|
|
|
private NetworkAdapter listener;
|
|
|
|
|
private Uri imageUri;
|
|
|
|
|
private String fileName;
|
|
|
|
|
private List<String> urls;
|
|
|
|
|
private List<Integer> deletedDays;
|
|
|
|
|
private ProgressDialog progressDialog;
|
|
|
|
|
private String filePath;
|
|
|
|
|
|
|
|
|
|
//static value to handle storage durations options
|
|
|
|
|
private static final int NEVER = 0;
|
|
|
|
@ -91,6 +87,13 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
deletedDays.add(YEAR);
|
|
|
|
|
|
|
|
|
|
updateServerList();
|
|
|
|
|
ImageButton resetButton = (ImageButton) findViewById(R.id.reset_button);
|
|
|
|
|
resetButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
resetImage();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//retrieve previous state if it exist
|
|
|
|
|
if(savedInstanceState != null) {
|
|
|
|
@ -104,21 +107,22 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//prepare the listener that handle upload result
|
|
|
|
|
listener = new NetworkListener() {
|
|
|
|
|
listener = new NetworkAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void fileUploaded(final Img image) {
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
//add uploaded img to history
|
|
|
|
|
Database.getInstance(getApplicationContext()).addImage(image);
|
|
|
|
|
Long id = Database.getInstance(getApplicationContext()).addImage(image);
|
|
|
|
|
//dismiss progressDialog
|
|
|
|
|
if(progressDialog!=null)
|
|
|
|
|
progressDialog.dismiss();
|
|
|
|
|
resetImage();
|
|
|
|
|
//launch LinkActivity
|
|
|
|
|
Intent linkIntent = new Intent(MainActivity.this,LinkActivity.class);
|
|
|
|
|
linkIntent.putExtra("URL", image.getUrl());
|
|
|
|
|
linkIntent.putExtra("short",image.getShortHash());
|
|
|
|
|
linkIntent.putExtra("imageId", id);
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
|
|
|
|
|
startActivity(linkIntent);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -131,8 +135,11 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
public void run() {
|
|
|
|
|
//display toast error
|
|
|
|
|
Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
|
|
|
|
|
if(progressDialog != null)
|
|
|
|
|
if(progressDialog != null) {
|
|
|
|
|
progressDialog.dismiss();
|
|
|
|
|
progressDialog = null;
|
|
|
|
|
}
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -143,8 +150,13 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
uploadBt.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
|
|
uploadImage();
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
|
|
|
|
|
uploadImage();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -161,6 +173,7 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
Intent receiveIntent = getIntent();
|
|
|
|
|
if((receiveIntent == null || receiveIntent.getType() == null || !receiveIntent.getType().contains("image/")) && imageUri == null) {
|
|
|
|
|
uploadBt.setVisibility(View.GONE);
|
|
|
|
|
resetButton.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
selectBt.setVisibility(View.GONE);
|
|
|
|
@ -213,16 +226,16 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
|
|
|
|
|
private void displayImage() {
|
|
|
|
|
if(imageUri != null) {
|
|
|
|
|
//retrieve the filePath of the image
|
|
|
|
|
ContentResolver contentResolver = getContentResolver();
|
|
|
|
|
Cursor cursor = contentResolver.query(imageUri, null, null, null, null);
|
|
|
|
|
cursor.moveToFirst();
|
|
|
|
|
filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
|
|
|
|
|
cursor.close();
|
|
|
|
|
//display it in the imageView
|
|
|
|
|
Bitmap bt = BitmapFactory.decodeFile(filePath);
|
|
|
|
|
ImageView view = (ImageView) findViewById(R.id.thumbnail_main);
|
|
|
|
|
view.setImageBitmap(bt);
|
|
|
|
|
Bitmap bt;
|
|
|
|
|
try {
|
|
|
|
|
bt = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
|
|
|
|
|
ImageView view = (ImageView) findViewById(R.id.thumbnail_main);
|
|
|
|
|
view.setImageBitmap(bt);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -231,7 +244,12 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
Spinner urlSpinner = (Spinner)findViewById(R.id.servers_spinner);
|
|
|
|
|
int pos = urlSpinner.getSelectedItemPosition();
|
|
|
|
|
if(urls.size() < pos) {
|
|
|
|
|
Toast.makeText(this,getString(R.string.server_list_error),Toast.LENGTH_LONG).show();
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
Toast.makeText(MainActivity.this, getString(R.string.server_list_error), Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String url = urls.get(pos);
|
|
|
|
@ -243,26 +261,24 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
|
|
|
|
|
//read image as bytes to upload it
|
|
|
|
|
byte[] bytearray = null;
|
|
|
|
|
FileInputStream fileStream = null;
|
|
|
|
|
//create a fileStream from the file path
|
|
|
|
|
if(filePath != null && !filePath.isEmpty()) {
|
|
|
|
|
File file = new File(filePath);
|
|
|
|
|
fileName = file.getName();
|
|
|
|
|
try {
|
|
|
|
|
fileStream = new FileInputStream(file);
|
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
//create a fileStream from the file path
|
|
|
|
|
InputStream stream = null;
|
|
|
|
|
try{
|
|
|
|
|
stream = getContentResolver().openInputStream(imageUri);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
//read data from the file and store it in a byte array
|
|
|
|
|
if(fileStream != null) {
|
|
|
|
|
if(stream != null) {
|
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
|
|
int readed = 0;
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
while(readed != -1) {
|
|
|
|
|
try {
|
|
|
|
|
readed = fileStream.read(buffer);
|
|
|
|
|
readed = stream.read(buffer);
|
|
|
|
|
if(readed != -1)
|
|
|
|
|
outStream.write(buffer,0,readed);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
@ -274,22 +290,40 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
}
|
|
|
|
|
//upload image and display a progress bar
|
|
|
|
|
if(bytearray != null && bytearray.length > 0) {
|
|
|
|
|
progressDialog = new ProgressDialog(MainActivity.this);
|
|
|
|
|
progressDialog.setMessage(getString(R.string.upload_progress));
|
|
|
|
|
progressDialog.setCancelable(false);
|
|
|
|
|
progressDialog.show();
|
|
|
|
|
NetworkManager.getInstance(listener, getApplicationContext()).upload(url, delete, fileName, bytearray);
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
progressDialog = new ProgressDialog(MainActivity.this);
|
|
|
|
|
progressDialog.setMessage(getString(R.string.upload_progress));
|
|
|
|
|
progressDialog.setCancelable(false);
|
|
|
|
|
progressDialog.show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
NetworkManager.getInstance(listener, getApplicationContext()).upload(url, delete, null, bytearray);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
listener.fileUploadError(getString(R.string.empty_file));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void requestFile() {
|
|
|
|
|
private void resetImage(){
|
|
|
|
|
imageUri = null;
|
|
|
|
|
ImageView view = (ImageView) findViewById(R.id.thumbnail_main);
|
|
|
|
|
view.setImageBitmap(null);
|
|
|
|
|
ImageButton resetButton = (ImageButton) findViewById(R.id.reset_button);
|
|
|
|
|
resetButton.setVisibility(View.GONE);
|
|
|
|
|
Button selectButton = (Button) findViewById(R.id.select_button);
|
|
|
|
|
selectButton.setVisibility(View.VISIBLE);
|
|
|
|
|
Button uploadButton = (Button) findViewById(R.id.upload_button);
|
|
|
|
|
uploadButton.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requestFile() {
|
|
|
|
|
//ask for image file
|
|
|
|
|
Intent requestFileIntent = new Intent(Intent.ACTION_PICK);
|
|
|
|
|
requestFileIntent.setType("image/*");
|
|
|
|
|
startActivityForResult(requestFileIntent, 0);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -297,11 +331,15 @@ public class MainActivity extends ActionBarActivity {
|
|
|
|
|
if(resultCode == RESULT_OK){
|
|
|
|
|
//retrieve uri from the request image activity and prepare
|
|
|
|
|
imageUri = returnIntent.getData();
|
|
|
|
|
|
|
|
|
|
Button uploadBt = (Button) findViewById(R.id.upload_button);
|
|
|
|
|
uploadBt.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
|
|
Button selectButton = (Button) findViewById(R.id.select_button);
|
|
|
|
|
selectButton.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
|
|
ImageButton resetButton = (ImageButton) findViewById(R.id.reset_button);
|
|
|
|
|
resetButton.setVisibility(View.VISIBLE);
|
|
|
|
|
displayImage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|