Commit e8a9d342 authored by Schoumi's avatar Schoumi

Add Server Management no way to pass from inactive to active or reverse

yet
parent f8aea37d
......@@ -12,7 +12,7 @@
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
......@@ -21,7 +21,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeviceActivity"></activity>
<activity
android:label="@string/device_informations"
android:name=".activity.DeviceActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:label="@string/server_management"
android:name=".activity.ServersActivity"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
\ No newline at end of file
package fr.mobdev.lpcprog;
package fr.mobdev.lpcprog.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -6,16 +6,21 @@ import android.widget.TextView;
import java.util.List;
import fr.mobdev.lpcprog.managers.IspManager;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.objects.USBDevice;
import fr.mobdev.lpcprog.managers.UsbCommManager;
public class DeviceActivity extends AppCompatActivity {
private UsbComm comm;
private UsbCommManager comm;
private USBDevice dev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device);
comm = UsbComm.getInstance(this);
comm = UsbCommManager.getInstance(this);
List<USBDevice> devices = comm.getDevices();
int pid = getIntent().getIntExtra("PID",-1);
int vid = getIntent().getIntExtra("VID",-1);
......
package fr.mobdev.lpcprog;
package fr.mobdev.lpcprog.activity;
import android.content.Intent;
import android.os.Bundle;
......@@ -11,13 +11,17 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.List;
import fr.mobdev.lpcprog.adapters.DeviceAdapter;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.objects.USBDevice;
import fr.mobdev.lpcprog.managers.UsbCommManager;
public class MainActivity extends AppCompatActivity {
private UsbComm comm;
private UsbCommManager comm;
private List<USBDevice> devices;
private int value = 0;
......@@ -28,7 +32,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
comm = UsbComm.getInstance(this);
comm = UsbCommManager.getInstance(this);
final GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener(){
@Override
......@@ -70,6 +74,10 @@ public class MainActivity extends AppCompatActivity {
updateList();
return true;
}
else if(id == R.id.action_server){
Intent newIntent = new Intent(this,ServersActivity.class);
startActivity(newIntent);
}
return super.onOptionsItemSelected(item);
}
......
/*
* 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.lpcprog.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.adapters.ServerAdapter;
import fr.mobdev.lpcprog.dialog.ServerDialog;
import fr.mobdev.lpcprog.listener.ServerListener;
import fr.mobdev.lpcprog.managers.DatabaseManager;
import fr.mobdev.lpcprog.objects.Server;
/*
* Activity that allow user to manage the server where he want to upload his images. Server must be lutim instance to work with the app
*/
public class ServersActivity extends AppCompatActivity {
private List<List<Server>> dbServers;
private ServerListener serverListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.servers);
Toolbar toolbar = (Toolbar) findViewById(R.id.servers_toolbar);
setSupportActionBar(toolbar);
/* ***** Change status of the repos: active to inactive or reverse *****
ListView serverList = (ListView) findViewById(R.id.servers_list);
serverList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Server clickedServer = dbServers.get(position);
DatabaseManager.getInstance(getApplicationContext()).changeServerStatus(clickedServer.id,!clickedServer.isActive);
updateServers();
return true;
}
});*/
serverListener = new ServerListener() {
@Override
public void updateServerList() {
updateServers();
}
};
updateServers();
}
private void updateServers()
{
// build the view with server list in database
dbServers = DatabaseManager.getInstance(getApplicationContext()).getServers();
List<String> namesList = new ArrayList<>();
namesList.add(getString(R.string.active_section));
namesList.add(getString(R.string.inactive_section));
namesList.add(getString(R.string.failed_section));
ServerAdapter adapter = new ServerAdapter(dbServers,namesList,serverListener);
RecyclerView serverList = (RecyclerView) findViewById(R.id.servers_list);
serverList.setLayoutManager(new LinearLayoutManager(this));
serverList.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_servers, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_add_server) {
ServerDialog serverDialog = new ServerDialog();
serverDialog.setServerListener(serverListener);
serverDialog.show(getSupportFragmentManager(),"Server Dialog");
return true;
}
return super.onOptionsItemSelected(item);
}
}
package fr.mobdev.lpcprog;
package fr.mobdev.lpcprog.adapters;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
......@@ -11,6 +8,9 @@ import android.widget.TextView;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.objects.USBDevice;
public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
private List<USBDevice> devices;
......
package fr.mobdev.lpcprog.adapters;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.listener.ServerListener;
import fr.mobdev.lpcprog.managers.DatabaseManager;
import fr.mobdev.lpcprog.objects.Server;
public class ServerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int SECTION = 0;
private static final int FIELD = 1;
private List<List<Server>> servers;
private List<String> sections;
private ServerListener serverListener;
private int count;
public ServerAdapter(List<List<Server>> servers, List<String> sections, ServerListener listener) {
this.servers = servers;
this.sections = sections;
serverListener = listener;
countElements();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == SECTION) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.server_section, parent, false);
return new SectionHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.server_item, parent, false);
return new ServerHolder(view,serverListener);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof SectionHolder) {
SectionHolder sectionHolder = (SectionHolder) holder;
int section = getSection(position);
System.out.println("pos " + position + " section " + section);
String sectionName = sections.get(section);
sectionHolder.setupSection(sectionName);
} else if (holder instanceof ServerHolder) {
ServerHolder serverHolder = (ServerHolder) holder;
int section = getSection(position);
int pos = getRealPosition(position);
Server server = servers.get(section).get(pos);
serverHolder.setupServer(server);
}
}
private void countElements() {
int size = 0;
for (int i = 0; i < sections.size(); i++) {
if (servers.get(i).size() > 0) {
size++;
size += servers.get(i).size();
}
}
count = size;
}
private int getRealPosition(int fakePosition) {
System.out.println("get real " + fakePosition);
fakePosition -= 1;
for (int i = 0; i < sections.size(); i++) {
int serverSize = servers.get(i).size();
if (fakePosition - serverSize < 0)
return fakePosition;
if (serverSize > 0) {
fakePosition -= 1;
fakePosition -= serverSize;
}
}
return -1;
}
private int getSection(int fakePosition) {
int prev_size = 1;
for (int i = 0; i < sections.size(); i++) {
int serverSize = servers.get(i).size();
if (fakePosition < prev_size + serverSize)
return i;
if (servers.get(i).size() > 0) {
prev_size += 1;
prev_size += serverSize;
}
}
return -1;//error
}
@Override
public int getItemCount() {
return count;
}
@Override
public int getItemViewType(int position) {
int nbValues = 0;
for (int i = 0; i < sections.size(); i++) {
if (position == i + nbValues)
return SECTION;
else if (position < i + nbValues)
break;
nbValues += servers.get(i).size();
}
return FIELD;
}
}
class ServerHolder extends RecyclerView.ViewHolder{
ServerListener serverListener;
public ServerHolder(View itemView, ServerListener serverListener) {
super(itemView);
this.serverListener = serverListener;
}
public void setupServer(final Server server){
TextView view = (TextView) itemView.findViewById(R.id.server_name);
view.setText(server.url.toString());
ImageView deleteView = (ImageView) itemView.findViewById(R.id.server_delete);
deleteView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setMessage(itemView.getContext().getString(R.string.delete_server_message)+" "+server.url)
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//delete server from database and update the view
DatabaseManager.getInstance(itemView.getContext()).deleteServer(server.id);
serverListener.updateServerList();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
class SectionHolder extends RecyclerView.ViewHolder {
public SectionHolder(View itemView) {
super(itemView);
}
public void setupSection(String sectionName) {
TextView view = (TextView)itemView.findViewById(R.id.section_name);
view.setText(sectionName);
}
}
/*
* 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.lpcprog.dialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.support.annotation.NonNull;
import fr.mobdev.lpcprog.listener.ServerListener;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.managers.DatabaseManager;
/*
* Dialog allow user to add a new Server where he can upload images
*/
public class ServerDialog extends DialogFragment {
private static final int HTTPS_POSITION = 1;
private ServerListener listener;
public void setServerListener(ServerListener listener)
{
this.listener = listener;
}
@Override
public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
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
DatabaseManager.getInstance(getActivity().getApplicationContext()).addServer(url);
listener.updateServerList();
dismiss();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
/*
* 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.lpcprog.listener;
import java.util.EventListener;
public interface ServerListener extends EventListener {
void updateServerList();
}
package fr.mobdev.lpcprog.managers;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.objects.Server;
public class DatabaseManager extends SQLiteOpenHelper {
private static DatabaseManager instance;
public static DatabaseManager getInstance(Context context){
if(instance == null)
instance = new DatabaseManager(context);
return instance;
}
private DatabaseManager(Context context){
super(context,"lpcprog.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table if not exists binaries (" +
"id integer primary key autoincrement, name 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), isActive INTEGER, attempted INTEGER);");
ContentValues values = new ContentValues();
values.put("url","https://wiki.mob-dev.fr/files/");
values.put("isActive",1);
values.put("attempted",0);
db.insert("servers",null,values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Nothing to do right now
}
public void addServer(String url) {
ContentValues values = new ContentValues();
values.put("url",url);
values.put("isActive",1);
values.put("attempted",0);
getWritableDatabase().insert("servers",null,values);
}
public void changeServerStatus(long id, boolean newStatus) {
ContentValues values = new ContentValues();
values.put("isActive",newStatus);
String where = "id = ?";
String whereArgs[] = new String[1];
whereArgs[0] = String.valueOf(id);
getWritableDatabase().update("servers",values,where,whereArgs);
}
public void deleteServer(long id)
{
String where = "id = ?";
String whereArgs[] = new String[1];
whereArgs[0] = String.valueOf(id);
getWritableDatabase().delete("servers",where,whereArgs);
}
public List<List<Server>> getServers() {
List<List<Server>> list = new ArrayList<>();
List<Server> active = new ArrayList<>();
List<Server> inactive = new ArrayList<>();
List<Server> failed = new ArrayList<>();
list.add(active);
list.add(failed);
list.add(inactive);
Cursor cursor = getReadableDatabase().query("servers", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int col = 0;
try {
Server server = new Server();
server.id = cursor.getLong(col++);
server.url = new URL(cursor.getString(col++));
int isActive = cursor.getInt(col++);
server.isActive = isActive != 0;
server.attempted = cursor.getInt(col);
if (server.isActive && server.attempted == 0)
active.add(server);
else if (server.isActive && server.attempted != 0)
failed.add(server);
else
inactive.add(server);
} catch (Exception e) {
e.printStackTrace();
}
}
cursor.close();
return list;
}
}
package fr.mobdev.lpcprog;
package fr.mobdev.lpcprog.managers;
import com.ftdi.j2xx.D2xxManager;
import com.ftdi.j2xx.FT_Device;
......@@ -6,6 +6,8 @@ import com.ftdi.j2xx.FT_Device;
import java.nio.ByteBuffer;
import java.util.Locale;
import fr.mobdev.lpcprog.objects.USBDevice;
public class IspManager {
//defines
......
package fr.mobdev.lpcprog.managers;
public class NetworkManager {
}
package fr.mobdev.lpcprog;
package fr.mobdev.lpcprog.managers;
import android.content.Context;
......@@ -7,16 +7,18 @@ import com.ftdi.j2xx.D2xxManager;
import java.util.ArrayList;
import java.util.List;
public class UsbComm {
import fr.mobdev.lpcprog.objects.USBDevice;
private static UsbComm instance = null;
public class UsbCommManager {
private static UsbCommManager instance = null;
private D2xxManager manager = null;
private Context context;
List<USBDevice> openedDevices;
private UsbComm(Context context){
private UsbCommManager(Context context){
openedDevices = new ArrayList<>();
try {
manager = D2xxManager.getInstance(context);
......@@ -26,9 +28,9 @@ public class UsbComm {
}
}
public static UsbComm getInstance(Context context){
public static UsbCommManager getInstance(Context context){
if(instance == null)
instance = new UsbComm(context);
instance = new UsbCommManager(context);
return instance;
}
......