Continue Cleanup on files and add GPLV3 Header in each source file

Add landscape to the main activity
This commit is contained in:
Schoumi 2015-05-20 20:39:46 +02:00
parent 9be5255cdc
commit 1b85bb2f14
8 changed files with 257 additions and 45 deletions

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim; package fr.mobdev.gobelim;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,6 +33,9 @@ import android.database.sqlite.SQLiteOpenHelper;
import fr.mobdev.gobelim.objects.Img; import fr.mobdev.gobelim.objects.Img;
import fr.mobdev.gobelim.objects.Server; import fr.mobdev.gobelim.objects.Server;
/*
* Helper than manage all access to the database
*/
public class Database extends SQLiteOpenHelper { public class Database extends SQLiteOpenHelper {
private static Database instance; private static Database instance;
@ -53,11 +74,13 @@ public class Database extends SQLiteOpenHelper {
} }
public List<Img> getHistory() { public List<Img> getHistory() {
List<Img> history = new ArrayList<Img>(); List<Img> history = new ArrayList<>();
//ask for history order by date
String orderBy = "date ASC"; String orderBy = "date ASC";
Cursor cursor = getReadableDatabase().query("history", null, null, null, null, null, orderBy); Cursor cursor = getReadableDatabase().query("history", null, null, null, null, null, orderBy);
while(cursor.moveToNext()) while(cursor.moveToNext())
{ {
//build Img history with data received from database
int col = 0; int col = 0;
long id = cursor.getLong(col++); long id = cursor.getLong(col++);
String url = cursor.getString(col++); String url = cursor.getString(col++);
@ -65,19 +88,22 @@ public class Database extends SQLiteOpenHelper {
String realShortHash = cursor.getString(col++); String realShortHash = cursor.getString(col++);
long timestamp = cursor.getLong(col++); long timestamp = cursor.getLong(col++);
int storageDuration = cursor.getInt(col++); int storageDuration = cursor.getInt(col++);
byte[] thumbData = cursor.getBlob(col++); byte[] thumbData = cursor.getBlob(col);
//convert Long date to Calendar
Calendar date = Calendar.getInstance(); Calendar date = Calendar.getInstance();
date.setTimeZone(TimeZone.getTimeZone("Europe/Paris")); date.setTimeZone(TimeZone.getDefault());
date.setTimeInMillis(timestamp); date.setTimeInMillis(timestamp);
Img img = new Img(id, url, shortHash, realShortHash, date, storageDuration, thumbData); Img img = new Img(id, url, shortHash, realShortHash, date, storageDuration, thumbData);
history.add(img); history.add(img);
} }
cursor.close();
return history; return history;
} }
//not used today but will be used in v2 version
public void deleteImg(List<Img> deletedList) { public void deleteImg(List<Img> deletedList) {
for(Img img : deletedList) for(Img img : deletedList)
{ {
@ -110,14 +136,14 @@ public class Database extends SQLiteOpenHelper {
public List<Server> getServers(boolean defaultFirst) public List<Server> getServers(boolean defaultFirst)
{ {
List<Server> servers = new ArrayList<Server>(); List<Server> servers = new ArrayList<>();
Cursor cursor = getReadableDatabase().query("servers", null, null, null, null, null, null); Cursor cursor = getReadableDatabase().query("servers", null, null, null, null, null, null);
while(cursor.moveToNext()) while(cursor.moveToNext())
{ {
int col = 0; int col = 0;
long id = cursor.getLong(col++); long id = cursor.getLong(col++);
String url = cursor.getString(col++); String url = cursor.getString(col++);
int defValue = cursor.getInt(col++); int defValue = cursor.getInt(col);
boolean isDefault = false; boolean isDefault = false;
if(defValue == 1) if(defValue == 1)
isDefault = true; isDefault = true;
@ -132,6 +158,7 @@ public class Database extends SQLiteOpenHelper {
} }
} }
cursor.close();
return servers; return servers;
} }

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim; package fr.mobdev.gobelim;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -27,7 +45,6 @@ public class NetworkManager {
private NetworkListener listener; private NetworkListener listener;
private static NetworkManager instance; private static NetworkManager instance;
private Context context; private Context context;
private static int id;
private NetworkManager(NetworkListener listener,Context context) private NetworkManager(NetworkListener listener,Context context)
{ {
@ -69,8 +86,7 @@ public class NetworkManager {
} }
public Img uploadImage(String siteUrl, int nbDays, String fileName, byte[] byteArray) { public Img uploadImage(String siteUrl, int nbDays, String fileName, byte[] byteArray) {
Log.v("Url",siteUrl);
//getData
URL url = null; URL url = null;
Img imgOutput = null; Img imgOutput = null;
try { try {
@ -79,68 +95,72 @@ public class NetworkManager {
e1.printStackTrace(); e1.printStackTrace();
} }
HttpURLConnection conn = null; HttpURLConnection conn;
InputStream stream = null; InputStream stream = null;
DataOutputStream request = null; DataOutputStream request = null;
try { try {
if(isConnectedToInternet(context)) if(isConnectedToInternet(context))
{ {
String attachmentFileName = fileName;
String crlf = "\r\n"; String crlf = "\r\n";
String hyphens = "--"; String hyphens = "--";
String boundary = "------------------------dd8a045fcc22b35c"; String boundary = "------------------------dd8a045fcc22b35c";
//check if there is a HTTP 301 Error
conn = (HttpURLConnection) url.openConnection(); if(url != null) {
conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode(); }
else {
listener.fileUploadError("Connection Failed");
return null;
}
String location = conn.getHeaderField("Location"); String location = conn.getHeaderField("Location");
if(location != null) { if(location != null) {
//if there is follow the new destination
siteUrl = location; siteUrl = location;
url = new URL(location); url = new URL(location);
} }
conn = (HttpURLConnection) url.openConnection(); conn = (HttpURLConnection) url.openConnection();
//prepare the connection for upload
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setUseCaches(false); conn.setUseCaches(false);
conn.setDoInput(true); conn.setDoInput(true);
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setRequestProperty("User-Agent", "Gobelim"); conn.setRequestProperty("User-Agent", "Gobelim");
// conn.setFixedLengthStreamingMode(byteArray.length);
// conn.setChunkedStreamingMode(0);
conn.setInstanceFollowRedirects(true);
//conn.set
conn.setRequestProperty("Expect", "100-continue"); conn.setRequestProperty("Expect", "100-continue");
conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
request = new DataOutputStream(conn.getOutputStream()); request = new DataOutputStream(conn.getOutputStream());
//ask for JSON answer
request.writeBytes(hyphens + boundary + crlf); request.writeBytes(hyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"format\"" + crlf); request.writeBytes("Content-Disposition: form-data; name=\"format\"" + crlf);
request.writeBytes(crlf); request.writeBytes(crlf);
request.writeBytes("json" + crlf); request.writeBytes("json" + crlf);
//ask for storage duration
request.writeBytes(hyphens + boundary + crlf); request.writeBytes(hyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"delete-day\"" + crlf); request.writeBytes("Content-Disposition: form-data; name=\"delete-day\"" + crlf);
request.writeBytes(crlf); request.writeBytes(crlf);
request.writeBytes(nbDays + crlf); request.writeBytes(nbDays + crlf);
//setup filename and say that octets follow
request.writeBytes(hyphens + boundary + crlf); request.writeBytes(hyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + attachmentFileName + "\"" + crlf); request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + crlf);
request.writeBytes("Content-Type: application/octet-stream" + crlf); request.writeBytes("Content-Type: application/octet-stream" + crlf);
request.writeBytes(crlf); request.writeBytes(crlf);
request.flush(); request.flush();
//write image data
request.write(byteArray); request.write(byteArray);
//finish the format http post packet
request.writeBytes(crlf); request.writeBytes(crlf);
request.writeBytes(hyphens + boundary + hyphens + crlf); request.writeBytes(hyphens + boundary + hyphens + crlf);
request.flush(); request.flush();
//get answer
stream = conn.getInputStream(); stream = conn.getInputStream();
} }
} catch (IOException e1) { } catch (IOException e1) {
@ -148,13 +168,14 @@ public class NetworkManager {
listener.fileUploadError("Network Error"); listener.fileUploadError("Network Error");
} }
/******************************************************************************/
if(stream != null) { if(stream != null) {
//prepare JSON reading
InputStreamReader isr = new InputStreamReader(stream); InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
boolean isReading = true; boolean isReading = true;
String data; String data;
String jsonStr = ""; String jsonStr = "";
//get all data in a String
do { do {
try { try {
data = br.readLine(); data = br.readLine();
@ -167,30 +188,32 @@ public class NetworkManager {
isReading = false; isReading = false;
} }
} while (isReading); } while (isReading);
//parseJSon
//parse JSON answer
try { try {
request.close(); request.close();
stream.close(); stream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("after read answer");
System.out.println(jsonStr);
try { try {
JSONObject rootObject = new JSONObject(jsonStr); // Parse the JSON to a JSONObject // Parse the JSON to a JSONObject
JSONObject msg = rootObject.getJSONObject("msg"); // Get the element object JSONObject rootObject = new JSONObject(jsonStr);
// Get msg (root) element
JSONObject msg = rootObject.getJSONObject("msg");
// is there an error?
if(msg.has("msg")) { if(msg.has("msg")) {
String error = msg.getString("msg"); String error = msg.getString("msg");
listener.fileUploadError(error); listener.fileUploadError(error);
return null; return null;
} }
else if(msg.has("short")) { else if(msg.has("short")) {
//retrieve useful data
String hashOutput = msg.getString("short"); String hashOutput = msg.getString("short");
String realHashOutput = msg.getString("real_short"); String realHashOutput = msg.getString("real_short");
imgOutput = new Img(0, siteUrl, hashOutput, realHashOutput, Calendar.getInstance(), nbDays, null); imgOutput = new Img(0, siteUrl, hashOutput, realHashOutput, Calendar.getInstance(), nbDays, null);
} }
} catch (JSONException e) { } catch (JSONException e) {
// JSON Parsing error
e.printStackTrace(); e.printStackTrace();
listener.fileUploadError("JSON Unreadable"); listener.fileUploadError("JSON Unreadable");
} }
@ -200,6 +223,7 @@ public class NetworkManager {
private boolean isConnectedToInternet(Context context) private boolean isConnectedToInternet(Context context)
{ {
//verify the connectivity
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) if (networkInfo != null)

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim.dialog; package fr.mobdev.gobelim.dialog;
import android.app.AlertDialog; import android.app.AlertDialog;
@ -5,21 +23,21 @@ import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.support.annotation.NonNull;
import fr.mobdev.gobelim.Database; import fr.mobdev.gobelim.Database;
import fr.mobdev.gobelim.listener.ServerListener; import fr.mobdev.gobelim.listener.ServerListener;
import fr.mobdev.gobelim.R; import fr.mobdev.gobelim.R;
/*
* Dialog allow user to add a new Server where he can upload images
*/
public class ServerDialog extends DialogFragment { public class ServerDialog extends DialogFragment {
private static final int HTTP_POSITION = 0;
private static final int HTTPS_POSITION = 1; private static final int HTTPS_POSITION = 1;
private ServerListener listener; private ServerListener listener;
public void setServerListener(ServerListener listener) public void setServerListener(ServerListener listener)
@ -28,28 +46,28 @@ public class ServerDialog extends DialogFragment {
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
{ {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater(); final View view = View.inflate(getActivity(),R.layout.server_dialog, null);
final View view = inflater.inflate(R.layout.server_dialog, null);
EditText urlText = (EditText) view.findViewById(R.id.url_text);
//mask for url?
builder.setView(view) builder.setView(view)
.setTitle(R.string.server_title) .setTitle(R.string.server_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
//build the url
String url = "http"; String url = "http";
//is it http or https?
Spinner httpSpinner = (Spinner) view.findViewById(R.id.http_spinner); Spinner httpSpinner = (Spinner) view.findViewById(R.id.http_spinner);
if(httpSpinner.getSelectedItemPosition() == HTTPS_POSITION) if(httpSpinner.getSelectedItemPosition() == HTTPS_POSITION)
url += "s"; url += "s";
url +="://"; url +="://";
//get the rest of the url
EditText urlText = (EditText) view.findViewById(R.id.url_text); EditText urlText = (EditText) view.findViewById(R.id.url_text);
if(urlText.getText().length() > 0) if(urlText.getText().length() > 0)
url += urlText.getText(); url += urlText.getText();
//add server to database
Database.getInstance(getActivity().getApplicationContext()).addServer(url); Database.getInstance(getActivity().getApplicationContext()).addServer(url);
listener.updateServerList(); listener.updateServerList();
dismiss(); dismiss();

View File

@ -1,7 +1,24 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim.listener; package fr.mobdev.gobelim.listener;
import java.util.EventListener; import java.util.EventListener;
import java.util.List;
import fr.mobdev.gobelim.objects.Img; import fr.mobdev.gobelim.objects.Img;

View File

@ -1,7 +1,25 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim.listener; package fr.mobdev.gobelim.listener;
import java.util.EventListener; import java.util.EventListener;
public interface ServerListener extends EventListener { public interface ServerListener extends EventListener {
public void updateServerList(); void updateServerList();
} }

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim.objects; package fr.mobdev.gobelim.objects;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -64,9 +82,7 @@ public class Img {
if(image != null) { if(image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream); image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray(); return stream.toByteArray();
return byteArray;
} }
else { else {
return null; return null;

View File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.gobelim.objects; package fr.mobdev.gobelim.objects;
public class Server { public class Server {

View File

@ -0,0 +1,74 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/upload_pict"
android:id="@+id/upload_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
/>
<Button
android:layout_above="@+id/upload_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_pict"
android:id="@+id/select_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
/>
<LinearLayout
android:id="@+id/spinners_layout"
android:orientation="horizontal"
android:layout_above="@+id/select_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:layout_weight="50"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/servers_spinner"
android:layout_marginLeft="25dp"
/>
<LinearLayout
android:layout_weight="50"
android:orientation="horizontal"
android:id="@+id/layout_days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center_vertical"
>
<TextView
android:text="@string/duration"
android:singleLine="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/delete_day_spinner"
android:entries="@array/deleted_days"
/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_above="@+id/spinners_layout"
android:id="@+id/thumbnail_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>