Commit 1b85bb2f authored by Schoumi's avatar Schoumi

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

Add landscape to the main activity
parent 9be5255c
/*
* 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;
import java.util.ArrayList;
......@@ -15,6 +33,9 @@ import android.database.sqlite.SQLiteOpenHelper;
import fr.mobdev.gobelim.objects.Img;
import fr.mobdev.gobelim.objects.Server;
/*
* Helper than manage all access to the database
*/
public class Database extends SQLiteOpenHelper {
private static Database instance;
......@@ -53,11 +74,13 @@ public class Database extends SQLiteOpenHelper {
}
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";
Cursor cursor = getReadableDatabase().query("history", null, null, null, null, null, orderBy);
while(cursor.moveToNext())
{
{
//build Img history with data received from database
int col = 0;
long id = cursor.getLong(col++);
String url = cursor.getString(col++);
......@@ -65,19 +88,22 @@ public class Database extends SQLiteOpenHelper {
String realShortHash = cursor.getString(col++);
long timestamp = cursor.getLong(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();
date.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
date.setTimeZone(TimeZone.getDefault());
date.setTimeInMillis(timestamp);
Img img = new Img(id, url, shortHash, realShortHash, date, storageDuration, thumbData);
history.add(img);
}
cursor.close();
return history;
}
//not used today but will be used in v2 version
public void deleteImg(List<Img> deletedList) {
for(Img img : deletedList)
{
......@@ -110,14 +136,14 @@ public class Database extends SQLiteOpenHelper {
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);
while(cursor.moveToNext())
{
int col = 0;
long id = cursor.getLong(col++);
String url = cursor.getString(col++);
int defValue = cursor.getInt(col++);
int defValue = cursor.getInt(col);
boolean isDefault = false;
if(defValue == 1)
isDefault = true;
......@@ -132,6 +158,7 @@ public class Database extends SQLiteOpenHelper {
}
}
cursor.close();
return servers;
}
......
/*
* 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;
import java.io.BufferedReader;
......@@ -27,7 +45,6 @@ public class NetworkManager {
private NetworkListener listener;
private static NetworkManager instance;
private Context context;
private static int id;
private NetworkManager(NetworkListener listener,Context context)
{
......@@ -69,8 +86,7 @@ public class NetworkManager {
}
public Img uploadImage(String siteUrl, int nbDays, String fileName, byte[] byteArray) {
Log.v("Url",siteUrl);
//getData
URL url = null;
Img imgOutput = null;
try {
......@@ -79,68 +95,72 @@ public class NetworkManager {
e1.printStackTrace();
}
HttpURLConnection conn = null;
HttpURLConnection conn;
InputStream stream = null;
DataOutputStream request = null;
try {
if(isConnectedToInternet(context))
{
String attachmentFileName = fileName;
String crlf = "\r\n";
String hyphens = "--";
String boundary = "------------------------dd8a045fcc22b35c";
conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
//check if there is a HTTP 301 Error
if(url != null) {
conn = (HttpURLConnection) url.openConnection();
}
else {
listener.fileUploadError("Connection Failed");
return null;
}
String location = conn.getHeaderField("Location");
if(location != null) {
//if there is follow the new destination
siteUrl = location;
url = new URL(location);
}
conn = (HttpURLConnection) url.openConnection();
//prepare the connection for upload
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("User-Agent", "Gobelim");
// conn.setFixedLengthStreamingMode(byteArray.length);
// conn.setChunkedStreamingMode(0);
conn.setInstanceFollowRedirects(true);
//conn.set
conn.setRequestProperty("Expect", "100-continue");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
request = new DataOutputStream(conn.getOutputStream());
//ask for JSON answer
request.writeBytes(hyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"format\"" + crlf);
request.writeBytes(crlf);
request.writeBytes("json" + crlf);
//ask for storage duration
request.writeBytes(hyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"delete-day\"" + crlf);
request.writeBytes(crlf);
request.writeBytes(nbDays + crlf);
//setup filename and say that octets follow
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(crlf);
request.flush();
//write image data
request.write(byteArray);
//finish the format http post packet
request.writeBytes(crlf);
request.writeBytes(hyphens + boundary + hyphens + crlf);
request.flush();
//get answer
stream = conn.getInputStream();
}
} catch (IOException e1) {
......@@ -148,13 +168,14 @@ public class NetworkManager {
listener.fileUploadError("Network Error");
}
/******************************************************************************/
if(stream != null) {
//prepare JSON reading
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr);
boolean isReading = true;
String data;
String jsonStr = "";
//get all data in a String
do {
try {
data = br.readLine();
......@@ -167,30 +188,32 @@ public class NetworkManager {
isReading = false;
}
} while (isReading);
//parseJSon
//parse JSON answer
try {
request.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("after read answer");
System.out.println(jsonStr);
try {
JSONObject rootObject = new JSONObject(jsonStr); // Parse the JSON to a JSONObject
JSONObject msg = rootObject.getJSONObject("msg"); // Get the element object
// Parse the JSON to a JSONObject
JSONObject rootObject = new JSONObject(jsonStr);
// Get msg (root) element
JSONObject msg = rootObject.getJSONObject("msg");
// is there an error?
if(msg.has("msg")) {
String error = msg.getString("msg");
listener.fileUploadError(error);
return null;
}
else if(msg.has("short")) {
//retrieve useful data
String hashOutput = msg.getString("short");
String realHashOutput = msg.getString("real_short");
imgOutput = new Img(0, siteUrl, hashOutput, realHashOutput, Calendar.getInstance(), nbDays, null);
}
} catch (JSONException e) {
// JSON Parsing error
e.printStackTrace();
listener.fileUploadError("JSON Unreadable");
}
......@@ -200,6 +223,7 @@ public class NetworkManager {
private boolean isConnectedToInternet(Context context)
{
//verify the connectivity
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null)
......
/*
* 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;
import android.app.AlertDialog;
......@@ -5,21 +23,21 @@ import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.support.annotation.NonNull;
import fr.mobdev.gobelim.Database;
import fr.mobdev.gobelim.listener.ServerListener;
import fr.mobdev.gobelim.R;
/*
* Dialog allow user to add a new Server where he can upload images
*/
public class ServerDialog extends DialogFragment {
private static final int HTTP_POSITION = 0;
private static final int HTTPS_POSITION = 1;
private ServerListener listener;
public void setServerListener(ServerListener listener)
......@@ -28,28 +46,28 @@ public class ServerDialog extends DialogFragment {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.server_dialog, null);
EditText urlText = (EditText) view.findViewById(R.id.url_text);
//mask for url?
final View view = View.inflate(getActivity(),R.layout.server_dialog, null);
builder.setView(view)
.setTitle(R.string.server_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//build the url
String url = "http";
//is it http or https?
Spinner httpSpinner = (Spinner) view.findViewById(R.id.http_spinner);
if(httpSpinner.getSelectedItemPosition() == HTTPS_POSITION)
url += "s";
url +="://";
//get the rest of the url
EditText urlText = (EditText) view.findViewById(R.id.url_text);
if(urlText.getText().length() > 0)
url += urlText.getText();
//add server to database
Database.getInstance(getActivity().getApplicationContext()).addServer(url);
listener.updateServerList();
dismiss();
......
/*
* 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;
import java.util.EventListener;
import java.util.List;
import fr.mobdev.gobelim.objects.Img;
......
/*
* 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;
import java.util.EventListener;
public interface ServerListener extends EventListener {
public void updateServerList();
void updateServerList();
}
/*
* 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;
import android.graphics.Bitmap;
......@@ -64,9 +82,7 @@ public class Img {
if(image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
return stream.toByteArray();
}
else {
return null;
......
/*
* 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;
public class Server {
......
<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>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment