Commit 181e3c15 authored by Schoumi's avatar Schoumi

Manage default delay from servers info

Fixes #23
parent ad3b1117
......@@ -43,7 +43,7 @@ public class Database extends SQLiteOpenHelper {
public static Database getInstance(Context context)
{
if(instance == null)
instance = new Database(context, "Lutim.db", null, 3);
instance = new Database(context, "Lutim.db", null, 4);
return instance;
}
......@@ -56,7 +56,7 @@ public class Database extends SQLiteOpenHelper {
db.execSQL("Create table if not exists history (" +
"id integer primary key autoincrement, url varchar(1024), short_hash varchar(1024), real_short_hash varchar(1024), date INTEGER, storage_duration INTEGER ,thumb TEXT, token varchar(1024));");
db.execSQL("Create table if not exists servers (" +
"id integer primary key autoincrement, url varchar(1024), isDefault INTEGER);");
"id integer primary key autoincrement, url varchar(1024), isDefault INTEGER, default_delay INTEGER, max_delay INTEGER, max_file_size INTEGER);");
ContentValues values = new ContentValues();
values.put("url","https://framapic.org");
......@@ -86,6 +86,26 @@ public class Database extends SQLiteOpenHelper {
{
db.execSQL("Alter table history add column token varchar(1024);");
}
if(oldVersion <= 3)
{
ContentValues values = new ContentValues();
values.clear();
values.put("url","https://wtf.roflcopter.fr/pics/");
values.put("isDefault",false);
db.insert("servers", null, values);
values.clear();
values.put("url","https://pic.infini.fr");
values.put("isDefault",false);
db.insert("servers", null, values);
values.clear();
values.put("url","https://pic.drakonix.net");
values.put("isDefault",false);
db.insert("servers", null, values);
db.execSQL("Alter table servers add column default_delay INTEGER;");
db.execSQL("Alter table servers add column max_delay INTEGER;");
db.execSQL("Alter table servers add column max_file_size INTEGER;");
}
}
public List<Img> getHistory(boolean descendingOrder) {
......@@ -159,11 +179,14 @@ public class Database extends SQLiteOpenHelper {
int col = 0;
long id = cursor.getLong(col++);
String url = cursor.getString(col++);
int defValue = cursor.getInt(col);
int defValue = cursor.getInt(col++);
int defaultDelay = cursor.getInt(col++);
int maxDelay = cursor.getInt(col++);
long maxFileSize = cursor.getLong(col);
boolean isDefault = false;
if(defValue == 1)
isDefault = true;
Server server = new Server(id, url, isDefault);
Server server = new Server(id, url, isDefault,maxDelay,defaultDelay,maxFileSize);
if(defaultFirst && isDefault)
{
servers.add(0,server);
......@@ -227,4 +250,16 @@ public class Database extends SQLiteOpenHelper {
cursor.close();
return image;
}
void updateServer(long id, int defaultDelay, int maxDelay, long maxFileSize) {
ContentValues values = new ContentValues();
String whereClause = "id = ?";
String[] whereArgs = new String[1];
whereArgs[0] = String.valueOf(id);
values.put("default_delay",defaultDelay);
values.put("max_delay",maxDelay);
values.put("max_file_size",maxFileSize);
getWritableDatabase().update("servers",values,whereClause,whereArgs);
}
}
......@@ -44,6 +44,7 @@ import android.provider.OpenableColumns;
import fr.mobdev.goblim.listener.NetworkListener;
import fr.mobdev.goblim.objects.Img;
import fr.mobdev.goblim.objects.Server;
public class NetworkManager {
......@@ -110,6 +111,17 @@ public class NetworkManager {
thread.addMessageQueue(mes);
}
public void updateDefaultServersConf(Context context, List<Server> servers) {
for(Server server : servers) {
NetworkThread.Message mes = new NetworkThread.Message();
mes.type = NetworkThread.Message_Type.GET_INFO;
mes.context = context;
mes.id = server.getId();
mes.url = server.getUrl();
addMessageToQueue(mes);
}
}
......@@ -167,6 +179,9 @@ class NetworkThread extends Thread{
case UPLOAD_IMG:
uploadImage(mes.context,mes.url,mes.nbDays,mes.imageUri,mes.fileNo,mes.fileNb);
break;
case GET_INFO:
getInfo(mes.context,mes.url,mes.id);
break;
}
} catch (InterruptedException e) {
......@@ -175,7 +190,59 @@ class NetworkThread extends Thread{
}
messages.remove(0);
}
}
private void getInfo(Context context, String serverUrl, long id) {
if (!isConnectedToInternet(context))
return;
URL url = null;
try {
if(!serverUrl.endsWith("/"))
serverUrl = serverUrl+"/";
serverUrl = serverUrl + "infos";
url = new URL(serverUrl);
} catch (Exception e) {
e.printStackTrace();
}
if (url != null) {
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection != null) {
InputStream stream = urlConnection.getInputStream();
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();
if (data != null)
jsonStr += data;
else
isReading = false;
} catch (IOException e) {
e.printStackTrace();
isReading = false;
}
} while (isReading);
JSONObject rootObject = new JSONObject(jsonStr);
if(rootObject.has("default_delay")) {
int defaultDelay = rootObject.getInt("default_delay");
long maxFileSize = rootObject.getLong("max_file_size");
int maxDelay = rootObject.getInt("max_delay");
Database.getInstance(context).updateServer(id,defaultDelay,maxDelay,maxFileSize);
}
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void deleteImage(final Context context, final String deleteUrl) {
......@@ -421,10 +488,12 @@ class NetworkThread extends Thread{
Uri imageUri;
int fileNo;
int fileNb;
long id;
}
enum Message_Type{
DELETE_IMG,
UPLOAD_IMG
UPLOAD_IMG,
GET_INFO
}
}
......@@ -46,6 +46,7 @@ import java.util.Locale;
import java.util.TimeZone;
import fr.mobdev.goblim.Database;
import fr.mobdev.goblim.NetworkManager;
import fr.mobdev.goblim.objects.Img;
import fr.mobdev.goblim.R;
......@@ -100,6 +101,8 @@ public class HistoryActivity extends AppCompatActivity {
}
});
NetworkManager.getInstance(null).updateDefaultServersConf(this,Database.getInstance(this).getServers(true));
}
@Override
......
......@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.List;
import fr.mobdev.goblim.Database;
import fr.mobdev.goblim.NetworkManager;
import fr.mobdev.goblim.dialog.ServerDialog;
import fr.mobdev.goblim.listener.ServerListener;
import fr.mobdev.goblim.objects.Server;
......@@ -132,6 +133,7 @@ public class ServersActivity extends AppCompatActivity {
ListView serverList = (ListView) findViewById(R.id.servers_list);
serverList.setAdapter(adapter);
NetworkManager.getInstance(null).updateDefaultServersConf(this,dbServers);
}
@Override
......
......@@ -38,6 +38,7 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
......@@ -72,6 +73,7 @@ public class UploadActivity extends AppCompatActivity {
private NetworkAdapter listener;
private ArrayList<Uri> uris;
private List<Server> servers;
private List<String> urls;
private List<Integer> deletedDays;
private ProgressDialog progressDialog;
......@@ -159,6 +161,41 @@ public class UploadActivity extends AppCompatActivity {
RecyclerView th = (RecyclerView) findViewById(R.id.imageList);
th.setAdapter(adapter);
Button uploadBt = (Button) findViewById(R.id.upload_button);
Spinner serverSpinner = (Spinner) findViewById(R.id.servers_spinner);
serverSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Server server = servers.get(position);
int defaultDelay = server.getDefautDelay();
int pos = 0;
int min = Integer.MAX_VALUE;
int savePos = 0;
for(Integer delay : deletedDays) {
if(delay == defaultDelay)
break;
int diff = delay - defaultDelay;
if(min > Math.abs(diff)) {
min = Math.abs(diff);
savePos = pos;
}
pos++;
}
Spinner deleteDaySpinner = (Spinner) findViewById(R.id.delete_day_spinner);
if(pos >= deletedDays.size())
deleteDaySpinner.setSelection(savePos);
else
deleteDaySpinner.setSelection(pos);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//retrieve previous state if it exist
if(savedInstanceState != null) {
int selectedServer = savedInstanceState.getInt("selectedServer");
......@@ -180,8 +217,7 @@ public class UploadActivity extends AppCompatActivity {
}
if (selectedServer < urls.size()) {
Spinner servers = (Spinner) findViewById(R.id.servers_spinner);
servers.setSelection(selectedServer);
serverSpinner.setSelection(selectedServer);
}
}
deleteCache();
......@@ -447,7 +483,7 @@ public class UploadActivity extends AppCompatActivity {
//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);
servers = Database.getInstance(getApplicationContext()).getServers(true);
urls.clear();
int pos = 0;
//create the string list of server name from database data
......@@ -587,7 +623,7 @@ public class UploadActivity extends AppCompatActivity {
infosLayout.setVisibility(View.GONE);
dataLayout.setVisibility(View.VISIBLE);
} else {
for(int i = 0; clip != null && i < clip.getItemCount(); i++) {
for(int i = 0; i < clip.getItemCount(); i++) {
Uri imageUri = clip.getItemAt(i).getUri();
if(imageUri == null)
continue;
......
......@@ -23,12 +23,18 @@ public class Server {
private long id;
private String url;
private boolean defaultServer;
private int maxDelay;
private int defaultDelay;
private long maxFileSize;
public Server(long id, String url, boolean defaultServer)
public Server(long id, String url, boolean defaultServer, int maxDelay, int defaultDelay, long maxFileSize)
{
this.id = id;
this.url = url;
this.defaultServer = defaultServer;
this.maxDelay = maxDelay;
this.defaultDelay = defaultDelay;
this.maxFileSize = maxFileSize;
}
public String getUrl()
......@@ -46,4 +52,15 @@ public class Server {
return defaultServer;
}
public int getMaxDelay() {
return maxDelay;
}
public long getMaxFileSize() {
return maxFileSize;
}
public int getDefautDelay() {
return defaultDelay;
}
}
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