|
|
|
@ -52,6 +52,12 @@ import fr.mobdev.gobelim.objects.Img; |
|
|
|
|
import fr.mobdev.gobelim.objects.Server; |
|
|
|
|
import fr.mobdev.gobelim.R; |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* Activity used to handle sharing pictures from other app that user want to upload on a lutim instance |
|
|
|
|
* if user launch the app by itself he can also pick a pictures from his device and upload it as well. |
|
|
|
|
* This Activity let user access to the others activities in order to manage history, servers and after an upload |
|
|
|
|
* the shared options of the given link |
|
|
|
|
*/ |
|
|
|
|
public class MainActivity extends ActionBarActivity { |
|
|
|
|
|
|
|
|
|
private NetworkListener listener; |
|
|
|
@ -60,7 +66,9 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
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; |
|
|
|
|
private static final int ONE = 1; |
|
|
|
|
private static final int SEVEN = 7; |
|
|
|
@ -72,6 +80,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
|
setContentView(R.layout.main); |
|
|
|
|
|
|
|
|
|
//prepare data used for upload
|
|
|
|
|
imageUri = null; |
|
|
|
|
urls = new ArrayList<>(); |
|
|
|
|
deletedDays = new ArrayList<>(); |
|
|
|
@ -82,6 +91,8 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
deletedDays.add(YEAR); |
|
|
|
|
|
|
|
|
|
updateServerList(); |
|
|
|
|
|
|
|
|
|
//retrieve previous state if it exist
|
|
|
|
|
if(savedInstanceState != null) { |
|
|
|
|
int selectedServer = savedInstanceState.getInt("selectedServer"); |
|
|
|
|
imageUri = savedInstanceState.getParcelable("imageURI"); |
|
|
|
@ -89,8 +100,10 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
Spinner servers = (Spinner) findViewById(R.id.servers_spinner); |
|
|
|
|
servers.setSelection(selectedServer); |
|
|
|
|
} |
|
|
|
|
displayImage(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//prepare the listener that handle upload result
|
|
|
|
|
listener = new NetworkListener() { |
|
|
|
|
@Override |
|
|
|
|
public void fileUploaded(final Img image) { |
|
|
|
@ -100,7 +113,8 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
//add uploaded img to history
|
|
|
|
|
Database.getInstance(getApplicationContext()).addImage(image); |
|
|
|
|
//dismiss progressDialog
|
|
|
|
|
progressDialog.dismiss(); |
|
|
|
|
if(progressDialog!=null) |
|
|
|
|
progressDialog.dismiss(); |
|
|
|
|
//launch LinkActivity
|
|
|
|
|
Intent linkIntent = new Intent(MainActivity.this,LinkActivity.class); |
|
|
|
|
linkIntent.putExtra("URL", image.getUrl()); |
|
|
|
@ -117,12 +131,14 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
public void run() { |
|
|
|
|
//display toast error
|
|
|
|
|
Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show(); |
|
|
|
|
progressDialog.dismiss(); |
|
|
|
|
if(progressDialog != null) |
|
|
|
|
progressDialog.dismiss(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//prepare for upload
|
|
|
|
|
Button uploadBt = (Button) findViewById(R.id.upload_button); |
|
|
|
|
uploadBt.setOnClickListener(new View.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
@ -141,14 +157,16 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
//have we receive image from share or do you need to ask it to the user
|
|
|
|
|
//have we receive image from share or do you need to ask it to the user if we haven't ask for it before (screen rotation)
|
|
|
|
|
Intent receiveIntent = getIntent(); |
|
|
|
|
if(receiveIntent == null || receiveIntent.getType() == null || !receiveIntent.getType().contains("image/")) { |
|
|
|
|
if((receiveIntent == null || receiveIntent.getType() == null || !receiveIntent.getType().contains("image/")) && imageUri == null) { |
|
|
|
|
uploadBt.setVisibility(View.GONE); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
selectBt.setVisibility(View.GONE); |
|
|
|
|
imageUri = receiveIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
|
|
|
|
if(receiveIntent != null && imageUri == null) { |
|
|
|
|
imageUri = receiveIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
|
|
|
|
} |
|
|
|
|
displayImage(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -162,6 +180,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onSaveInstanceState(Bundle savedInstanceState) { |
|
|
|
|
//save imageURI and selected server position
|
|
|
|
|
savedInstanceState.putParcelable("imageURI", imageUri); |
|
|
|
|
Spinner selectedServer = (Spinner) findViewById(R.id.servers_spinner); |
|
|
|
|
int pos = selectedServer.getSelectedItemPosition(); |
|
|
|
@ -171,11 +190,13 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
|
|
|
|
|
private void updateServerList() { |
|
|
|
|
Spinner serversSpinner = (Spinner) findViewById(R.id.servers_spinner); |
|
|
|
|
//retrieve the selected server name in case it change his place in list
|
|
|
|
|
String selectedServer = (String) serversSpinner.getSelectedItem(); |
|
|
|
|
|
|
|
|
|
List<Server> servers = Database.getInstance(getApplicationContext()).getServers(true); |
|
|
|
|
urls.clear(); |
|
|
|
|
int pos = 0; |
|
|
|
|
//create the string list of server name from database data
|
|
|
|
|
for(Server server : servers) { |
|
|
|
|
if(server.getUrl().equals(selectedServer)) { |
|
|
|
|
pos = urls.size(); |
|
|
|
@ -186,53 +207,22 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_dropdown_item_1line,urls); |
|
|
|
|
serversSpinner.setAdapter(adapter); |
|
|
|
|
|
|
|
|
|
//select the previous selected server
|
|
|
|
|
serversSpinner.setSelection(pos); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void displayImage() { |
|
|
|
|
FileInputStream fileStream = null; |
|
|
|
|
File file; |
|
|
|
|
if(imageUri != null) { |
|
|
|
|
//retrieve the filePath of the image
|
|
|
|
|
ContentResolver contentResolver = getContentResolver(); |
|
|
|
|
Cursor cursor = contentResolver.query(imageUri, null, null, null, null); |
|
|
|
|
cursor.moveToFirst(); |
|
|
|
|
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); |
|
|
|
|
filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); |
|
|
|
|
cursor.close(); |
|
|
|
|
file = new File(filePath); |
|
|
|
|
fileName = file.getName(); |
|
|
|
|
try { |
|
|
|
|
fileStream = new FileInputStream(file); |
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if(fileStream != null) { |
|
|
|
|
try { |
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
|
|
|
|
int readed = 0; |
|
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
|
while(readed != -1) { |
|
|
|
|
try { |
|
|
|
|
readed = fileStream.read(buffer); |
|
|
|
|
if(readed != -1) |
|
|
|
|
outStream.write(buffer,0,readed); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
readed = -1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
byte[] bytearray = outStream.toByteArray(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bitmap bt = BitmapFactory.decodeByteArray(bytearray,0,bytearray.length); |
|
|
|
|
ImageView view = (ImageView) findViewById(R.id.thumbnail_main); |
|
|
|
|
view.setImageBitmap(bt); |
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
//display it in the imageView
|
|
|
|
|
Bitmap bt = BitmapFactory.decodeFile(filePath); |
|
|
|
|
ImageView view = (ImageView) findViewById(R.id.thumbnail_main); |
|
|
|
|
view.setImageBitmap(bt); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -246,18 +236,16 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
} |
|
|
|
|
String url = urls.get(pos); |
|
|
|
|
|
|
|
|
|
//How long the server need to store image
|
|
|
|
|
Spinner deleteSpinner = (Spinner)findViewById(R.id.delete_day_spinner); |
|
|
|
|
pos = deleteSpinner.getSelectedItemPosition(); |
|
|
|
|
int delete = deletedDays.get(pos); |
|
|
|
|
|
|
|
|
|
//read image as bytes to upload it
|
|
|
|
|
byte[] bytearray = null; |
|
|
|
|
FileInputStream fileStream = null; |
|
|
|
|
if(imageUri != null) { |
|
|
|
|
ContentResolver contentResolver = getContentResolver(); |
|
|
|
|
Cursor cursor = contentResolver.query(imageUri, null, null, null, null); |
|
|
|
|
cursor.moveToFirst(); |
|
|
|
|
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); |
|
|
|
|
cursor.close(); |
|
|
|
|
//create a fileStream from the file path
|
|
|
|
|
if(filePath != null && !filePath.isEmpty()) { |
|
|
|
|
File file = new File(filePath); |
|
|
|
|
fileName = file.getName(); |
|
|
|
|
try { |
|
|
|
@ -267,6 +255,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//read data from the file and store it in a byte array
|
|
|
|
|
if(fileStream != null) { |
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
|
|
|
|
int readed = 0; |
|
|
|
@ -283,6 +272,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
} |
|
|
|
|
bytearray = outStream.toByteArray(); |
|
|
|
|
} |
|
|
|
|
//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)); |
|
|
|
@ -296,6 +286,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void requestFile() { |
|
|
|
|
//ask for image file
|
|
|
|
|
Intent requestFileIntent = new Intent(Intent.ACTION_PICK); |
|
|
|
|
requestFileIntent.setType("image/*"); |
|
|
|
|
startActivityForResult(requestFileIntent, 0); |
|
|
|
@ -304,6 +295,7 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
@Override |
|
|
|
|
public void onActivityResult(int requestCode, int resultCode,Intent returnIntent) { |
|
|
|
|
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); |
|
|
|
@ -323,18 +315,19 @@ public class MainActivity extends ActionBarActivity { |
|
|
|
|
@Override |
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
|
|
|
|
int id = item.getItemId(); |
|
|
|
|
|
|
|
|
|
Intent newIntent = null; |
|
|
|
|
if (id == R.id.action_manage_server) { |
|
|
|
|
Intent serverIntent = new Intent(this,ServersActivity.class); |
|
|
|
|
startActivity(serverIntent); |
|
|
|
|
return true; |
|
|
|
|
newIntent = new Intent(this,ServersActivity.class); |
|
|
|
|
} |
|
|
|
|
else if (id == R.id.action_show_history){ |
|
|
|
|
Intent historyIntent = new Intent(this,HistoryActivity.class); |
|
|
|
|
startActivity(historyIntent); |
|
|
|
|
newIntent = new Intent(this,HistoryActivity.class); |
|
|
|
|
} |
|
|
|
|
if(newIntent != null) |
|
|
|
|
{ |
|
|
|
|
startActivity(newIntent); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |