Enhance flow for flashing

Add Browse repository capacity
Fixes #10 #4
master
Schoumi 6 years ago
parent 12b8fc3648
commit 011b586c6b
  1. 171
      app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java
  2. 161
      app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java
  3. 23
      app/src/main/java/fr/mobdev/lpcprog/adapters/DeviceAdapter.java
  4. 2
      app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java
  5. 75
      app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java
  6. 148
      app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java
  7. 359
      app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java
  8. 392
      app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java
  9. 77
      app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java
  10. 2
      app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java
  11. 2
      app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java
  12. 1
      app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java
  13. 2
      app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java
  14. 42
      app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java
  15. 50
      app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java
  16. 115
      app/src/main/res/layout/binary_item.xml
  17. 64
      app/src/main/res/layout/device.xml
  18. 10
      app/src/main/res/layout/device_item.xml
  19. 164
      app/src/main/res/layout/flash.xml
  20. 24
      app/src/main/res/layout/home.xml
  21. 24
      app/src/main/res/layout/repository.xml
  22. 1
      app/src/main/res/layout/server_section.xml
  23. 21
      app/src/main/res/layout/usb_list.xml
  24. 4
      app/src/main/res/menu/main_drawer.xml
  25. 8
      app/src/main/res/menu/menu_main.xml
  26. 15
      app/src/main/res/values/strings.xml

@ -18,6 +18,9 @@
package fr.mobdev.lpcprog.activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -25,9 +28,6 @@ import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
@ -39,11 +39,15 @@ import android.view.MenuItem;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.dialog.ServerDialog;
import fr.mobdev.lpcprog.fragment.AddPartFragment;
import fr.mobdev.lpcprog.fragment.DeviceFragment;
import fr.mobdev.lpcprog.fragment.BrowseRepositoryFragment;
import fr.mobdev.lpcprog.fragment.FlashFragment;
import fr.mobdev.lpcprog.fragment.HomeFragment;
import fr.mobdev.lpcprog.fragment.PartsFragment;
import fr.mobdev.lpcprog.fragment.ServersFragment;
import fr.mobdev.lpcprog.fragment.USBListFragment;
import fr.mobdev.lpcprog.fragment.BrowseDeviceFragment;
import fr.mobdev.lpcprog.listener.ServerListener;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
import fr.mobdev.lpcprog.objects.USBDevice;
/*
@ -52,10 +56,15 @@ import fr.mobdev.lpcprog.objects.USBDevice;
*/
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,
USBListFragment.USBInteractionListener,
AddPartFragment.OnAddInteractionListener
{
BrowseDeviceFragment.BrowseDeviceListener deviceListener;
BrowseRepositoryFragment.BrowseRepositoryListener repositoryListener;
Binary binary;
Server server;
USBDevice device;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -68,9 +77,29 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
drawer.addDrawerListener(toggle);
toggle.syncState();
final FragmentManager manager = getSupportFragmentManager();
final FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = USBListFragment.newInstance();
Fragment frag = HomeFragment.newInstance(new HomeFragment.HomeListener() {
@Override
public void onBrowseRepositoryClick() {
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = BrowseRepositoryFragment.newInstance(repositoryListener);
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_container, frag);
transaction.commit();
updateMenu(null);
}
@Override
public void onBrowseDeviceClick() {
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = BrowseDeviceFragment.newInstance(deviceListener);
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_container, frag);
transaction.commit();
updateMenu(null);
}
});
transaction.replace(R.id.fragment_container, frag);
transaction.commit();
@ -81,16 +110,71 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
@Override
public void onReceive(Context context, Intent intent) {
Fragment fragment = manager.findFragmentById(R.id.fragment_container);
if(fragment instanceof USBListFragment) {
((USBListFragment)fragment).updateDevice(null);
if(fragment instanceof BrowseDeviceFragment) {
((BrowseDeviceFragment)fragment).updateDevice(null);
}
else if(fragment instanceof DeviceFragment){
manager.popBackStack();
else if(fragment instanceof FlashFragment){
if(intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_DETACHED")) {
//TODO Add dialog deconnected device
binary = null;
server = null;
device = null;
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
}
},filter);
deviceListener = new BrowseDeviceFragment.BrowseDeviceListener() {
@Override
public void onDeviceClick(USBDevice device) {
MainActivity.this.device = device;
if(binary != null) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = FlashFragment.newInstance(device,server, binary);
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
updateMenu(null);
} else {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = BrowseRepositoryFragment.newInstance(repositoryListener);
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
updateMenu(null);
}
}
};
repositoryListener = new BrowseRepositoryFragment.BrowseRepositoryListener() {
@Override
public void onBinaryClick(Server server, Binary binary) {
MainActivity.this.server = server;
MainActivity.this.binary = binary;
if(device != null) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = FlashFragment.newInstance(device,server, binary);
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
updateMenu(null);
} else {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = BrowseDeviceFragment.newInstance(deviceListener);
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
updateMenu(null);
}
}
};
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@ -102,6 +186,15 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
FragmentManager manager = getFragmentManager();
Fragment frag = manager.findFragmentById(R.id.fragment_container);
if(frag instanceof BrowseDeviceFragment) {
device = null;
}
else if(frag instanceof BrowseRepositoryFragment) {
binary = null;
server = null;
}
updateMenu(null);
}
}
@ -110,20 +203,18 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
Toolbar bar = (Toolbar) findViewById(R.id.toolbar);
Menu menu = bar.getMenu();
MenuItem add = menu.findItem(R.id.action_add_new);
MenuItem refresh = menu.findItem(R.id.action_refresh);
if(frag == null) {
FragmentManager manager = getSupportFragmentManager();
FragmentManager manager = getFragmentManager();
frag = manager.findFragmentById(R.id.fragment_container);
}
if(frag instanceof USBListFragment || frag instanceof DeviceFragment) {
add.setVisible(false);
refresh.setVisible(true);
} else if(frag instanceof PartsFragment || frag instanceof ServersFragment) {
if(frag instanceof PartsFragment || frag instanceof ServersFragment) {
add.setVisible(true);
refresh.setVisible(false);
if(frag instanceof PartsFragment)
add.setTitle(R.string.add_parts);
else
add.setTitle(R.string.add_server);
} else {
add.setVisible(false);
refresh.setVisible(false);
}
}
@ -137,18 +228,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentManager manager = getSupportFragmentManager();
FragmentManager manager = getFragmentManager();
final Fragment frag = manager.findFragmentById(R.id.fragment_container);
if (id == R.id.action_refresh) {
if(frag instanceof USBListFragment) {
((USBListFragment) frag).updateDevice(null);
} else if (frag instanceof DeviceFragment) {
((DeviceFragment) frag).updateRepositories(null);
}
} else if (id == R.id.action_add_new) {
if (id == R.id.action_add_new) {
if(frag instanceof ServersFragment) {
ServerDialog serverDialog = new ServerDialog();
serverDialog.setServerListener(new ServerListener() {
@Override
@ -165,7 +249,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
transaction.commit();
updateMenu(fragment);
}
}
return super.onOptionsItemSelected(item);
}
@ -174,16 +257,15 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment frag = null;
final FragmentManager manager = getSupportFragmentManager();
final FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
final Fragment old = manager.findFragmentById(R.id.fragment_container);
if(id == R.id.nav_devices) {
if(!(old instanceof USBListFragment)) {
frag = USBListFragment.newInstance();
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
}
if(id == R.id.nav_home) {
manager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
binary = null;
server = null;
device = null;
updateMenu(null);
} else if (id == R.id.nav_servers) {
if(!(old instanceof ServersFragment)) {
frag = ServersFragment.newInstance();
@ -225,16 +307,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
return true;
}
@Override
public void onUSBDeviceClick(USBDevice device) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = DeviceFragment.newInstance(device.PID,device.VID);
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
}
@Override
@ -245,7 +318,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.O
@Override
public void onSubmit() {
onBackPressed();
FragmentManager manager = getSupportFragmentManager();
FragmentManager manager = getFragmentManager();
Fragment frag = manager.findFragmentById(R.id.fragment_container);
if(frag instanceof PartsFragment) {
((PartsFragment)frag).updateList();

@ -19,28 +19,21 @@
package fr.mobdev.lpcprog.adapters;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
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.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.File;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.listener.DownloadListener;
import fr.mobdev.lpcprog.listener.FlashListener;
import fr.mobdev.lpcprog.fragment.BrowseRepositoryFragment;
import fr.mobdev.lpcprog.managers.DatabaseManager;
import fr.mobdev.lpcprog.managers.IspManager;
import fr.mobdev.lpcprog.managers.NetworkManager;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
/*
@ -54,12 +47,10 @@ public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private List<Server> servers;
private int count;
private Activity activity;
private FlashListener listener;
private BrowseRepositoryFragment.BrowseRepositoryListener listener;
public BinariesAdapter(List<Server> servers, Activity activity, FlashListener listener) {
public BinariesAdapter(List<Server> servers, BrowseRepositoryFragment.BrowseRepositoryListener listener) {
this.servers = servers;
this.activity = activity;
this.listener = listener;
countElements();
}
@ -72,7 +63,14 @@ public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
return new SectionHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.binary_item, parent, false);
return new BinaryHolder(view);
final BinaryHolder viewHolder = new BinaryHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onBinaryClick(viewHolder.server,viewHolder.binary);
}
});
return viewHolder;
}
}
@ -81,6 +79,8 @@ public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
if (holder instanceof SectionHolder) {
SectionHolder sectionHolder = (SectionHolder) holder;
int section = getSection(position);
if(section == -1)
return;
String sectionName = servers.get(section).url.toString();
sectionHolder.setupSection(sectionName);
} else if (holder instanceof BinaryHolder) {
@ -88,9 +88,11 @@ public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
int section = getSection(position);
int pos = getRealPosition(position);
if(section == -1 || pos == -1)
return;
Server server = servers.get(section);
Binary binary = servers.get(section).binaries.get(pos);
binaryHolder.setupBinary(server,binary,activity,listener);
binaryHolder.setupBinary(server,binary);
}
}
@ -153,114 +155,35 @@ public class BinariesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
class BinaryHolder extends RecyclerView.ViewHolder{
private DownloadListener listener;
Binary binary;
Server server;
BinaryHolder(View itemView) {
super(itemView);
}
@SuppressLint("SetTextI18n")
void setupBinary(final Server server, final Binary binary, final Activity activity, final FlashListener flashListener){
TextView name = (TextView) itemView.findViewById(R.id.binary_name);
TextView version = (TextView) itemView.findViewById(R.id.binary_version);
void setupBinary(final Server server, final Binary binary){
this.binary = binary;
this.server = server;
TextView name = itemView.findViewById(R.id.binary_name);
TextView version = itemView.findViewById(R.id.binary_version);
final LinearLayout downloadedLayout = (LinearLayout) itemView.findViewById(R.id.layout_downloaded);
final ImageView deleteView = (ImageView) itemView.findViewById(R.id.delete_download);
final ImageView flashView = (ImageView) itemView.findViewById(R.id.flash_download);
final ImageView downloadView = (ImageView) itemView.findViewById(R.id.download);
final ImageView stopView = (ImageView) itemView.findViewById(R.id.cancel_download);
final ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.progress_download);
final ImageView deleteView = itemView.findViewById(R.id.delete_download);
final ImageView downloadView = itemView.findViewById(R.id.download);
name.setText(binary.name);
version.setText("V "+binary.version);
//TODO Fix Internationalisation warning
version.setText("Vers. "+binary.version);
if(binary.isDownloaded) {
downloadView.setVisibility(View.GONE);
downloadedLayout.setVisibility(View.VISIBLE);
stopView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
} else if(binary.isDownloading){
downloadView.setVisibility(View.GONE);
downloadedLayout.setVisibility(View.GONE);
stopView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
} else {
downloadView.setVisibility(View.VISIBLE);
downloadedLayout.setVisibility(View.GONE);
stopView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
deleteView.setVisibility(View.VISIBLE);
} else {
downloadView.setVisibility(View.GONE);
deleteView.setVisibility(View.GONE);
}
listener = new DownloadListener() {
@Override
public void downloadCanceled(Server server, Binary binary) {
binary.isDownloading = false;
binary.progress = 0;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
downloadView.setVisibility(View.VISIBLE);
downloadedLayout.setVisibility(View.GONE);
stopView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
});
}
@Override
public void downloadSuccessful(Server server, Binary binary) {
binary.isDownloading = false;
binary.progress = 0;
binary.isDownloaded = true;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
downloadView.setVisibility(View.GONE);
downloadedLayout.setVisibility(View.VISIBLE);
stopView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
});
}
@Override
public void downloadError(Server server, Binary binary, final String error) {
binary.isDownloading = false;
binary.progress = 0;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
downloadView.setVisibility(View.VISIBLE);
downloadedLayout.setVisibility(View.GONE);
stopView.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
Snackbar.make(activity.findViewById(R.id.bin_list), error,Snackbar.LENGTH_LONG);
}
});
}
@Override
public void progress(Server server, Binary binary, final int progress) {
binary.progress = progress;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
}
});
}
};
downloadView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binary.isDownloading = true;
binary.progress = 0;
NetworkManager.getInstance(null,itemView.getContext()).retrieveBinary(server,binary,listener);
}
});
deleteView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -280,10 +203,8 @@ class BinaryHolder extends RecyclerView.ViewHolder{
binary.isDownloaded = false;
DatabaseManager.getInstance(itemView.getContext()).deleteBinary(binary.id);
//file and reset all downloaded items
downloadView.setVisibility(View.VISIBLE);
downloadedLayout.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
stopView.setVisibility(View.GONE);
downloadView.setVisibility(View.GONE);
deleteView.setVisibility(View.GONE);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@ -302,18 +223,6 @@ class BinaryHolder extends RecyclerView.ViewHolder{
alert.show();
}
});
flashView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
IspManager.getInstance(itemView.getContext()).flashBinary(server,binary,flashListener,flashView);
}
}).start();
}
});
}
}

@ -28,6 +28,7 @@ import android.widget.TextView;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.fragment.BrowseDeviceFragment;
import fr.mobdev.lpcprog.objects.USBDevice;
/*
@ -35,15 +36,25 @@ import fr.mobdev.lpcprog.objects.USBDevice;
*/
public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
BrowseDeviceFragment.BrowseDeviceListener listener;
private List<USBDevice> devices;
public DeviceAdapter(List<USBDevice> devices){
public DeviceAdapter(List<USBDevice> devices, BrowseDeviceFragment.BrowseDeviceListener listener){
this.devices = devices;
this.listener = listener;
}
@Override
public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item,parent,false);
return new DeviceHolder(view);
final DeviceHolder holder = new DeviceHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onDeviceClick(holder.usbDevice);
}
});
return holder;
}
@Override
@ -59,6 +70,7 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> {
class DeviceHolder extends RecyclerView.ViewHolder {
USBDevice usbDevice;
DeviceHolder(View itemView) {
super(itemView);
@ -66,11 +78,12 @@ class DeviceHolder extends RecyclerView.ViewHolder {
@SuppressLint("SetTextI18n")
void setupDevice(USBDevice device){
TextView name = (TextView) itemView.findViewById(R.id.device_name);
usbDevice = device;
TextView name = itemView.findViewById(R.id.device_name);
name.setText(itemView.getContext().getString(R.string.dev_name) + device.description);
TextView vendor = (TextView) itemView.findViewById(R.id.device_vendor_id);
TextView vendor = itemView.findViewById(R.id.device_vendor_id);
vendor.setText(itemView.getContext().getString(R.string.vendor_id)+String.format("%04x",device.VID));
TextView product = (TextView) itemView.findViewById(R.id.device_id);
TextView product = itemView.findViewById(R.id.device_id);
product.setText(itemView.getContext().getString(R.string.product_id)+String.format("%04x", device.PID));
}

@ -18,9 +18,9 @@
package fr.mobdev.lpcprog.fragment;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;

@ -18,14 +18,12 @@
package fr.mobdev.lpcprog.fragment;
import android.content.Context;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
@ -39,18 +37,20 @@ import fr.mobdev.lpcprog.managers.UsbCommManager;
/*
Fragment use to display USB device plugged through OTG port
*/
public class USBListFragment extends Fragment {
public class BrowseDeviceFragment extends Fragment {
private List<USBDevice> devices;
private USBInteractionListener listener;
private BrowseDeviceListener listener;
private UsbCommManager comm;
public USBListFragment() {
public BrowseDeviceFragment() {
}
public static USBListFragment newInstance() {
return new USBListFragment();
public static BrowseDeviceFragment newInstance(BrowseDeviceListener browseDeviceListener) {
BrowseDeviceFragment fragment = new BrowseDeviceFragment();
fragment.setBrowseDeviceListener(browseDeviceListener);
return fragment;
}
@ -61,30 +61,16 @@ public class USBListFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.usb_list,container,false);
final View v = inflater.inflate(R.layout.usb_list,container,false);
RecyclerView list = v.findViewById(R.id.device_list);
list.setLayoutManager(new LinearLayoutManager(getActivity()));
comm = UsbCommManager.getInstance(getActivity());
final GestureDetector gestureDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener(){
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public boolean onSingleTapUp(MotionEvent e){
return true;
}
});
RecyclerView view = (RecyclerView) v.findViewById(R.id.device_list);
view.setLayoutManager(new LinearLayoutManager(getContext()));
view.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e){
View v = view.findChildViewUnder(e.getX(), e.getY());
if(v != null && gestureDetector.onTouchEvent(e)){
int pos = view.getChildAdapterPosition(v);
onItemClick(pos);
return true;
}
return false;
public void onRefresh() {
updateDevice(v);
}
});
updateDevice(v);
@ -96,31 +82,18 @@ public class USBListFragment extends Fragment {
if(v == null)
v = getView();
assert v != null;
DeviceAdapter adapter = new DeviceAdapter(devices);
RecyclerView view = (RecyclerView) v.findViewById(R.id.device_list);
DeviceAdapter adapter = new DeviceAdapter(devices,listener);
RecyclerView view = v.findViewById(R.id.device_list);
view.setAdapter(adapter);
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setRefreshing(false);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (USBInteractionListener) context;
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
private void onItemClick(int pos){
if(devices.size()>pos){
listener.onUSBDeviceClick(devices.get(pos));
}
public void setBrowseDeviceListener(BrowseDeviceListener browseDeviceListener) {
listener = browseDeviceListener;
}
public interface USBInteractionListener {
void onUSBDeviceClick(USBDevice device);
public interface BrowseDeviceListener {
void onDeviceClick(USBDevice device);
}
}

@ -0,0 +1,148 @@
package fr.mobdev.lpcprog.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.adapters.BinariesAdapter;
import fr.mobdev.lpcprog.listener.NetworkListener;
import fr.mobdev.lpcprog.managers.DatabaseManager;
import fr.mobdev.lpcprog.managers.NetworkManager;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
public class BrowseRepositoryFragment extends Fragment {
private BrowseRepositoryListener browseListener;
private List<Server> servers;
public static BrowseRepositoryFragment newInstance(BrowseRepositoryListener browseRepositoryListener) {
BrowseRepositoryFragment fragment = new BrowseRepositoryFragment();
fragment.setBrowseRepositoryListener(browseRepositoryListener);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
servers = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.repository,container,false);
RecyclerView list = v.findViewById(R.id.bin_list);
list.setLayoutManager(new LinearLayoutManager(getActivity()));
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateRepositories(v);
}
});
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
updateRepositories(view);
}
public void setBrowseRepositoryListener(BrowseRepositoryListener browseRepositoryListener) {
browseListener = browseRepositoryListener;
}
public void updateRepositories(View view){
if(view == null)
view = getView();
final View v = view;
assert v != null;
servers.clear();
final NetworkListener listener = new NetworkListener() {
@Override
public void startBinaries() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setRefreshing(true);
}
});
}
@Override
public void startServer(Server server) {
}
@Override
public void endServer(Server server) {
if(!server.binaries.isEmpty())
servers.add(server);
}
@Override
public void endBinaries() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
RecyclerView list = v.findViewById(R.id.bin_list);
BinariesAdapter adapter = new BinariesAdapter(servers,browseListener);
list.setAdapter(adapter);
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setRefreshing(false);
}
});
}
@Override
public void onError(final String error) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
final Snackbar msg = Snackbar.make(v.findViewById(R.id.swipe_refresh), error,Snackbar.LENGTH_LONG);
msg.show();
List<List<Server>> localServers = DatabaseManager.getInstance(getActivity()).getServers(getActivity());
servers.addAll(localServers.get(0));
servers.addAll(localServers.get(1));
servers.addAll(localServers.get(2));
RecyclerView list = v.findViewById(R.id.bin_list);
BinariesAdapter adapter = new BinariesAdapter(servers,browseListener);
list.setAdapter(adapter);
SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.swipe_refresh);
swipeRefresh.setRefreshing(false);
}
});
}
};
NetworkManager.getInstance(listener).browseBinaries(getActivity());
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if(servers != null)
updateRepositories(null);
}
public interface BrowseRepositoryListener {
void onBinaryClick(Server server, Binary binary);
}
}

@ -1,359 +0,0 @@
/*
* Copyright (C) 2016 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.fragment;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
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.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.adapters.BinariesAdapter;
import fr.mobdev.lpcprog.listener.FlashListener;
import fr.mobdev.lpcprog.listener.NetworkListener;
import fr.mobdev.lpcprog.managers.DatabaseManager;
import fr.mobdev.lpcprog.managers.IspManager;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.managers.NetworkManager;
import fr.mobdev.lpcprog.objects.Server;
import fr.mobdev.lpcprog.objects.USBDevice;
import fr.mobdev.lpcprog.managers.UsbCommManager;
/*
Fragment that handle action on a specific device that can be flash with some Binaries
*/
public class DeviceFragment extends Fragment {
private UsbCommManager comm;
private USBDevice dev;
private List<Server> servers;
private long part_id;
private FlashListener flashListener;
public DeviceFragment() {
}
public static DeviceFragment newInstance(int PID, int VID) {
DeviceFragment frag = new DeviceFragment();
Bundle args = new Bundle();
args.putInt("PID",PID);
args.putInt("VID",VID);
frag.setArguments(args);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("SetTextI18n")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
final View v = inflater.inflate(R.layout.device,container,false);
servers = new ArrayList<>();
comm = UsbCommManager.getInstance(getActivity());
part_id = 0;
List<USBDevice> devices = comm.getDevices();
int pid = getArguments().getInt("PID",-1);
int vid = getArguments().getInt("VID",-1);
for(USBDevice device : devices){
if(device.PID == pid && device.VID == vid){
dev = device;
break;
}
}
if(dev != null) {
TextView name = (TextView) v.findViewById(R.id.name);
name.setText(getString(R.string.dev_name)+ dev.description);
TextView vendor = (TextView) v.findViewById(R.id.vendor_id);
vendor.setText(getString(R.string.vendor_id)+String.format("%04x", dev.VID));
TextView product = (TextView) v.findViewById(R.id.id);
product.setText(getString(R.string.product_id)+String.format("%04x", dev.PID));
}
doUsb();
ProgressBar bar = (ProgressBar) v.findViewById(R.id.progress_browse);
bar.setVisibility(View.GONE);
RecyclerView list = (RecyclerView) v.findViewById(R.id.bin_list);
list.setLayoutManager(new LinearLayoutManager(getContext()));
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setCancelable(false);
dialog.setTitle(R.string.flash_progress);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("toto");
flashListener = new FlashListener() {
@Override
public void onStartFlash() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.show();
}
});
}
@Override
public void onSuccess() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.flash_succeed);
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xFF000000);
}
});
alert.show();
}
});
}
@Override
public void onProgress(final int step, final int endStep, final int progress, final int step_res_id) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.setMax(100);
dialog.setProgress(progress);
dialog.setMessage(getString(R.string.step)+" "+String.valueOf(step)+"/"+String.valueOf(endStep)+" "+getString(step_res_id));
dialog.show();
}
});
}
@Override
public void onError(final String error, final ImageView view) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(error);
builder.setPositiveButton(R.string.retry_browse, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
view.performClick();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xFF000000);
alert.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(0xFF000000);
}
});
alert.show();
}
});
}
};
updateRepositories(v);
return v;
}
private void doUsb(){
new Thread(new Runnable() {
@Override
public void run() {
boolean conn = comm.openConnection(dev);
IspManager manager = IspManager.getInstance(getActivity());
String uidStr = "No UID Found or error";
String bootStr = "No Boot Version Found or error";
String partidStr = "No Part ID Found or error";
if(conn && manager.setupDevice(dev, 115200)) {
if(!manager.synchronizeIfPossible(10000)) {
updateIDS(uidStr,partidStr,bootStr);
getActivity().runOnUiThread(new Thread(new Runnable() {
@Override
public void run() {
View v = getView();
if(v == null)
return;
final Snackbar msg = Snackbar.make(v.findViewById(R.id.bin_list), getString(R.string.can_read_uid),Snackbar.LENGTH_INDEFINITE);
msg.setAction(R.string.retry_browse, new View.OnClickListener() {
@Override
public void onClick(View v) {
msg.dismiss();
doUsb();
}
});
msg.show();
}
}));
return;
}
String[] version = manager.readBootVersion();
String partid = manager.readPartId();
String[] uids = manager.readUid();
if (uids != null) {
uidStr = String.format("UID: %08x - %08x - %08x - %08x",Long.parseLong(uids[0]),
Long.parseLong(uids[1]),
Long.parseLong(uids[2]),
Long.parseLong(uids[3]));
}
if (version != null) {
bootStr = "Boot Version: " + version[0] + "." + version[1];
}
if(partid != null){
partidStr = String.format("Part Id %08x",Long.parseLong(partid));
part_id = Long.parseLong(partid);
IspManager.getInstance(getActivity()).setPartId(part_id);
}
}
updateIDS(uidStr,partidStr,bootStr);
}
}).start();
}
@SuppressLint("N")
private void updateIDS(final String uid, final String partid, final String boot) {
final View v = getView();
assert v != null;
if(getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
TextView view = (TextView) v.findViewById(R.id.uids);
view.setText(uid);
view = (TextView) v.findViewById(R.id.part_id);
view.setText(partid);
view = (TextView) v.findViewById(R.id.boot_version);
view.setText(boot);
}
});
}
}
public void updateRepositories(View view){
if(view == null)
view = getView();
final View v = view;
assert v != null;
servers.clear();
final NetworkListener listener = new NetworkListener() {
@Override
public void startBinaries() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ProgressBar bar = (ProgressBar) v.findViewById(R.id.progress_browse);
bar.setVisibility(View.VISIBLE);
RecyclerView list = (RecyclerView) v.findViewById(R.id.bin_list);
list.setVisibility(View.GONE);
}
});
}
@Override
public void startServer(Server server) {
}
@Override
public void endServer(Server server) {
if(!server.binaries.isEmpty())
servers.add(server);
}
@Override
public void endBinaries() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ProgressBar bar = (ProgressBar) v.findViewById(R.id.progress_browse);
bar.setVisibility(View.GONE);
RecyclerView list = (RecyclerView) v.findViewById(R.id.bin_list);
list.setVisibility(View.VISIBLE);
BinariesAdapter adapter = new BinariesAdapter(servers, getActivity(),flashListener);
list.setAdapter(adapter);
}
});
}
@Override
public void onError(final String error) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ProgressBar bar = (ProgressBar) v.findViewById(R.id.progress_browse);
bar.setVisibility(View.GONE);
final Snackbar msg = Snackbar.make(v.findViewById(R.id.bin_list), error,Snackbar.LENGTH_INDEFINITE);
msg.setAction(R.string.retry_browse, new View.OnClickListener() {
@Override
public void onClick(View v) {
msg.dismiss();
updateRepositories(null);
}
});
msg.show();
List<List<Server>> localServers = DatabaseManager.getInstance(getActivity()).getServers(getActivity());
servers.addAll(localServers.get(0));
servers.addAll(localServers.get(1));
servers.addAll(localServers.get(2));
RecyclerView list = (RecyclerView) v.findViewById(R.id.bin_list);
list.setVisibility(View.VISIBLE);
BinariesAdapter adapter = new BinariesAdapter(servers,getActivity(),flashListener);
list.setAdapter(adapter);
}
});
}
};
NetworkManager.getInstance(listener,getActivity()).browseBinaries();
}
}

@ -0,0 +1,392 @@
package fr.mobdev.lpcprog.fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.listener.DownloadListener;
import fr.mobdev.lpcprog.listener.FlashListener;
import fr.mobdev.lpcprog.managers.IspManager;
import fr.mobdev.lpcprog.managers.NetworkManager;
import fr.mobdev.lpcprog.managers.UsbCommManager;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
import fr.mobdev.lpcprog.objects.USBDevice;
public class FlashFragment extends Fragment{
private USBDevice device;
private Binary binary;
private Server server;
private Long part_id;
private String uidStr;
private String bootStr;
private String partidStr;
private String flash_infoStr="";
private TextView flash_info;
private ProgressBar progressBar;
private FlashListener flashListener;
public static FlashFragment newInstance(USBDevice device, Server server, Binary binary) {
FlashFragment fragment = new FlashFragment();
fragment.setDevice(device);
fragment.setBinary(server, binary);
return fragment;
}
@SuppressLint