Browse Source

Enhance flow for flashing

Add Browse repository capacity
Fixes #10 #4
tags/release-v1.2
Schoumi 2 years ago
parent
commit
011b586c6b
26 changed files with 1158 additions and 799 deletions
  1. 122
    49
      app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java
  2. 35
    126
      app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java
  3. 18
    5
      app/src/main/java/fr/mobdev/lpcprog/adapters/DeviceAdapter.java
  4. 1
    1
      app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java
  5. 24
    51
      app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java
  6. 148
    0
      app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java
  7. 0
    359
      app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java
  8. 392
    0
      app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java
  9. 77
    0
      app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java
  10. 1
    1
      app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java
  11. 1
    1
      app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java
  12. 0
    1
      app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java
  13. 1
    1
      app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java
  14. 21
    21
      app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java
  15. 14
    36
      app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java
  16. 51
    64
      app/src/main/res/layout/binary_item.xml
  17. 0
    64
      app/src/main/res/layout/device.xml
  18. 9
    1
      app/src/main/res/layout/device_item.xml
  19. 164
    0
      app/src/main/res/layout/flash.xml
  20. 24
    0
      app/src/main/res/layout/home.xml
  21. 24
    0
      app/src/main/res/layout/repository.xml
  22. 1
    0
      app/src/main/res/layout/server_section.xml
  23. 12
    9
      app/src/main/res/layout/usb_list.xml
  24. 2
    2
      app/src/main/res/menu/main_drawer.xml
  25. 1
    7
      app/src/main/res/menu/menu_main.xml
  26. 15
    0
      app/src/main/res/values/strings.xml

+ 122
- 49
app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java View File

@@ -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();

+ 35
- 126
app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java View File

@@ -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();
}
});
}
}


+ 18
- 5
app/src/main/java/fr/mobdev/lpcprog/adapters/DeviceAdapter.java View File

@@ -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));

}

+ 1
- 1
app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java View File

@@ -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;

app/src/main/java/fr/mobdev/lpcprog/fragment/USBListFragment.java → app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java View File

@@ -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);
}
}

+ 148
- 0
app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java View File

@@ -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);
}

}

+ 0
- 359
app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java View File

@@ -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();
}
}

+ 392
- 0
app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java View File

@@ -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("SetTextI18n")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.flash,container,false);
TextView name = v.findViewById(R.id.name);
name.setText(getString(R.string.dev_name)+ device.description);
TextView vendor = v.findViewById(R.id.vendor_id);
vendor.setText(getString(R.string.vendor_id)+String.format("%04x", device.VID));
TextView product = v.findViewById(R.id.id);
product.setText(getString(R.string.product_id)+String.format("%04x", device.PID));

TextView binaryName = v.findViewById(R.id.binary_name);
binaryName.setText(binary.name);
TextView binaryVersion = v.findViewById(R.id.binary_version);
binaryVersion.setText(getString(R.string.version)+" "+binary.version);
TextView serverName = v.findViewById(R.id.server);
serverName.setText(server.url.toString());

flash_info = v.findViewById(R.id.flash_information);

final Button btisp = v.findViewById(R.id.retry_isp_mode);
btisp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getISPInfo();
btisp.setVisibility(View.GONE);
}
});
btisp.setVisibility(View.GONE);

final Button btflash = v.findViewById(R.id.flash_device);
btflash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flashBinary();
btflash.setVisibility(View.GONE);
}
});
btflash.setVisibility(View.GONE);

final Button btdown = v.findViewById(R.id.retry_download);
btdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadBinary();
btdown.setVisibility(View.GONE);
}
});
btdown.setVisibility(View.GONE);

progressBar = v.findViewById(R.id.progress_flash);
progressBar.setVisibility(View.GONE);

flashListener = new FlashListener() {

int previousStep = 0;
int previousId = 0;
@Override
public void onStartFlash() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Button bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
updateProgress(0);
}
});
}

@Override
public void onSuccess() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Button bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
updateProgress(0);
flash_infoStr += getString(R.string.ok)+"\n";
flash_info.setText(flash_infoStr);

TextView step = v.findViewById(R.id.flash_steps);
step.setVisibility(View.GONE);
}
});
}

@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() {
if(previousStep != step || previousId != step_res_id) {
if(previousStep != 0)
flash_infoStr += getString(R.string.ok)+"\n";
previousStep = step;
previousId = step_res_id;
flash_infoStr += getString(R.string.step)+" "+step+"/"+endStep+" "+getString(step_res_id)+" ";
flash_info.setText(flash_infoStr);

TextView stepV = v.findViewById(R.id.flash_steps);
stepV.setVisibility(View.VISIBLE);
stepV.setText(getString(R.string.step)+" "+step+"/"+endStep+" "+getString(step_res_id));
if(step == previousStep) {
previousStep = 0;
previousId = 0;
}
}
updateProgress(progress);
}
});
}

@Override
public void onError(final String error) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
flash_infoStr += getString(R.string.fail)+"\n";
flash_infoStr += error+"\n";
flash_info.setText(flash_infoStr);
Button bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
TextView step = v.findViewById(R.id.flash_steps);
step.setVisibility(View.GONE);
}
});
}
};

return v;
}

private void flashBinary() {
new Thread(new Runnable() {
@Override
public void run() {
IspManager.getInstance(getActivity()).flashBinary(server,binary,flashListener);
}
}).start();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getISPInfo();
}

private void getISPInfo(){

View v = getView();
assert v != null;

final TextView step = v.findViewById(R.id.flash_steps);
step.setVisibility(View.VISIBLE);
step.setText(getString(R.string.retrieve_lpc_info));

flash_infoStr += getString(R.string.retrieve_lpc_info)+" ";
flash_info.setText(flash_infoStr);
progressBar.setVisibility(View.VISIBLE);

new Thread(new Runnable() {
@Override
public void run() {
UsbCommManager comm = UsbCommManager.getInstance(getActivity());
boolean conn = comm.openConnection(device);
IspManager manager = IspManager.getInstance(getActivity());
uidStr = "No UID Found or error";
bootStr = "No Boot Version Found or error";
partidStr = "No Part ID Found or error";
boolean status = true;
if (conn && manager.setupDevice(device, 115200)) {
if (!manager.synchronizeIfPossible(10000)) {
status = false;
} else {
updateProgress(0);
String[] version = manager.readBootVersion();
updateProgress(33);
String partid = manager.readPartId();
updateProgress(67);
String[] uids = manager.readUid();
updateProgress(100);

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]));
} else {
status = false;
}
if (version != null) {
bootStr = "Boot Version: " + version[0] + "." + version[1];
} else {
status = false;
}
if (partid != null) {
partidStr = String.format("Part Id: %08x", Long.parseLong(partid));
part_id = Long.parseLong(partid);
IspManager.getInstance(getActivity()).setPartId(part_id);
} else {
status = false;
}
}
}
final boolean statusLPCInfo = status;
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(!statusLPCInfo) {
View v = getView();
Button bt = v.findViewById(R.id.retry_isp_mode);
bt.setVisibility(View.VISIBLE);
flash_infoStr += getString(R.string.fail) + "\n";
flash_info.setText(flash_infoStr);
} else {
View v = getView();
Button bt = v.findViewById(R.id.retry_isp_mode);
bt.setVisibility(View.GONE);
bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.GONE);
flash_infoStr += getString(R.string.ok) + "\n";
flash_info.setText(flash_infoStr);
downloadBinary();
}
updateIDS(uidStr, partidStr, bootStr);
progressBar.setVisibility(View.GONE);
step.setVisibility(View.GONE);
}
});

}
}
}).start();
}

private void updateProgress(final int i) {
if(getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(i);
}
});
}
}

private void downloadBinary() {
final View v = getView();
assert v != null;

final TextView step = v.findViewById(R.id.flash_steps);
step.setVisibility(View.VISIBLE);
step.setText(getString(R.string.download_binary));

progressBar.setVisibility(View.VISIBLE);

flash_infoStr += getString(R.string.download_binary)+" ";
flash_info.setText(flash_infoStr);

if(binary.isDownloaded) {
step.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);

flash_infoStr += getString(R.string.ok)+"\n";
flash_info.setText(flash_infoStr);
Button bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.VISIBLE);
return;
}

DownloadListener listener = new DownloadListener() {

@Override
public void downloadSuccessful(Server server, Binary binary) {
binary.isDownloading = false;
binary.progress = 0;
binary.isDownloaded = true;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
step.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
flash_infoStr += getString(R.string.ok)+"\n";
flash_info.setText(flash_infoStr);
Button bt = v.findViewById(R.id.flash_device);
bt.setVisibility(View.VISIBLE);
}
});
}

@Override
public void downloadError(Server server, Binary binary, final String error) {
binary.isDownloading = false;
binary.progress = 0;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
step.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
flash_infoStr += getString(R.string.fail)+"\n";
flash_infoStr += error+"\n";
flash_info.setText(flash_infoStr);

Button bt = v.findViewById(R.id.retry_download);
bt.setVisibility(View.VISIBLE);
}
});
}

@Override
public void progress(Server server, Binary binary, final int progress) {
binary.progress = progress;
updateProgress(progress);
}
};

NetworkManager.getInstance(null).retrieveBinary(getActivity(),server,binary,listener);
}

private void updateIDS(String uid, String partid, String boot) {
final View v = getView();
assert v != null;
TextView view = v.findViewById(R.id.uids);
view.setText(uid);

view = v.findViewById(R.id.part_id);
view.setText(partid);

view = v.findViewById(R.id.boot_version);
view.setText(boot);
}



private void setBinary(Server server, Binary binary) {
this.server = server;
this.binary = binary;
}

public void setDevice(USBDevice device) {
this.device = device;
}
}

+ 77
- 0
app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2017 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.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import fr.mobdev.lpcprog.R;

/*
Main Fragment that Handle navigation threw device or repository in the Flashing Flow
*/
public class HomeFragment extends Fragment{

private HomeListener homeListener;

public static HomeFragment newInstance(HomeListener listener) {
HomeFragment fragment = new HomeFragment();
fragment.setHomeListener(listener);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home, container, false);
Button bt = v.findViewById(R.id.browse_repo);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
homeListener.onBrowseRepositoryClick();
}
});
bt = v.findViewById(R.id.browse_device);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
homeListener.onBrowseDeviceClick();
}
});
return v;
}

public void setHomeListener(HomeListener homeListener) {
this.homeListener = homeListener;
}

public interface HomeListener {
void onBrowseRepositoryClick();
void onBrowseDeviceClick();
}

}

+ 1
- 1
app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java View File

@@ -18,8 +18,8 @@

package fr.mobdev.lpcprog.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

+ 1
- 1
app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java View File

@@ -18,8 +18,8 @@

package fr.mobdev.lpcprog.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

+ 0
- 1
app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java View File

@@ -28,7 +28,6 @@ import fr.mobdev.lpcprog.objects.Server;
*/

public interface DownloadListener extends EventListener {
void downloadCanceled(Server server, Binary binary);
void downloadSuccessful(Server server, Binary binary);
void downloadError(Server server, Binary binary, String string);
void progress(Server server, Binary binary, int progress);

+ 1
- 1
app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java View File

@@ -29,5 +29,5 @@ public interface FlashListener extends EventListener{
void onStartFlash();
void onSuccess();
void onProgress(int step, int endStep, int progress, int step_res_id);
void onError(String string, ImageView imageView);
void onError(String string);
}

+ 21
- 21
app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java View File

@@ -205,90 +205,90 @@ public class IspManager {
return version;
}

public int flashBinary(Server server, Binary binary, FlashListener listener, ImageView flashView) {
public int flashBinary(Server server, Binary binary, FlashListener listener) {
Part parts = DatabaseManager.getInstance(context).getPart(part_id);
listener.onStartFlash();
if(parts == null) {
listener.onError(context.getString(R.string.parts_missing),flashView);
listener.onError(context.getString(R.string.parts_missing));
return -1;
}
int sector_size = parts.flash_size / parts.flash_nb_sectors;
long ram_addr = parts.ram_base_addr + parts.ram_buffer_offset;
boolean uuencode = parts.uuencode;
byte data[];
listener.onProgress(1,5,100,R.string.sanity_check);
listener.onProgress(1,6,100,R.string.sanity_check);
//Sanity Checks
if (ram_addr > parts.ram_base_addr + parts.ram_size) {
listener.onError(context.getString(R.string.buffer_out_ram),flashView);
listener.onError(context.getString(R.string.buffer_out_ram));
return -2;
}

long write_size = calcWriteSize(sector_size, parts.ram_buffer_size);
if (write_size == 0) {
listener.onError(context.getString(R.string.block_size_null),flashView);
listener.onError(context.getString(R.string.block_size_null));
return -3;
}

listener.onProgress(2,5,0,R.string.erase_flash);
listener.onProgress(2,6,0,R.string.erase_flash);
int ret = eraseFlash(parts,listener);
if (ret != 0) {
listener.onError(context.getString(R.string.cant_erase_flash),flashView);
listener.onError(context.getString(R.string.cant_erase_flash));
return -4;
}


listener.onProgress(3,5,0,R.string.read_file);
listener.onProgress(3,6,0,R.string.read_file);
data = new byte[parts.flash_size];
//copy file buffer to data array
File file = new File(context.getApplicationInfo().dataDir + "/" + server.id + "/" + binary.path + binary.filename);
int size = copy_buffer(file, data, parts.flash_size);
if (size <= 0) {
listener.onError(context.getString(R.string.copy_file),flashView);
listener.onError(context.getString(R.string.copy_file));
return -5;
}
listener.onProgress(3,5,100,R.string.read_file);
listener.onProgress(3,6,100,R.string.read_file);

fill_data_with_0(data, size, parts.flash_size);
int cksum = calc_binary_checksum(data);
setChecksum(data, 28, cksum);
listener.onProgress(1,2,0,R.string.verify_checksum);
listener.onProgress(4,6,0,R.string.verify_checksum);
if(!verifyChecksum(data,cksum)) {
listener.onError(context.getString(R.string.checksum_failed),flashView);
listener.onError(context.getString(R.string.checksum_failed));
return -6;
}
listener.onProgress(1,2,100,R.string.verify_checksum);
listener.onProgress(4,6,100,R.string.verify_checksum);
long blocks = (size / write_size) + ((size % write_size == 0) ? 0 : 1);
if (blocks * write_size > parts.flash_size) {
listener.onError(context.getString(R.string.flash_outside_end),flashView);
listener.onError(context.getString(R.string.flash_outside_end));
}

listener.onProgress(4,5,0,R.string.write_to_flash);
listener.onProgress(5,6,0,R.string.write_to_flash);
for(int i = 0; i < blocks; i++){
listener.onProgress(4,5,(int)(i*100/blocks),R.string.write_to_flash);
listener.onProgress(5,6,(int)(i*100/blocks),R.string.write_to_flash);
int current_sector = (int)(i*write_size)/sector_size;
int flash_addr = (int)(parts.flash_base_addr + (i*write_size));
//prepare-for-write
ret = send_cmd_sectors('P',current_sector,current_sector);
if(ret != 0){
listener.onError(context.getString(R.string.prepare_write_error)+" "+i,flashView);
listener.onError(context.getString(R.string.prepare_write_error)+" "+i);
return ret;
}

ret = send_buf_to_ram(data,(int)(i*write_size),(int)ram_addr,(int)write_size, uuencode);
if(ret != 0) {
listener.onError(context.getString(R.string.write_ram_error)+" "+i,flashView);
listener.onError(context.getString(R.string.write_ram_error)+" "+i);
return ret;
}

//write-to-ram
ret = send_cmd_address('C', flash_addr, (int)ram_addr, (int) write_size);
if(ret != 0){
listener.onError(context.getString(R.string.copy_to_flash_error)+" "+i,flashView);
listener.onError(context.getString(R.string.copy_to_flash_error)+" "+i);
return ret;
}

}
listener.onProgress(5,5,100,R.string.end_flash);
listener.onProgress(6,6,100,R.string.end_flash);
listener.onSuccess();

return ret;
@@ -502,7 +502,7 @@ public class IspManager {
String str = new String(data,0,2);
str = str.replace("\r", "");
str = str.replace("\n", "");
if (!str.matches("[0-9 -+]*"))
if (!str.matches("[0-9 -+]*")||str.isEmpty())
return -1;
return Integer.parseInt(str);
}

+ 14
- 36
app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java View File

@@ -48,20 +48,15 @@ public class NetworkManager {

private NetworkListener listener;
private static NetworkManager instance;
private Context context;
private boolean downloadInProgress;
private List<Binary> cancelList;

private NetworkManager(NetworkListener listener, Context context)
private NetworkManager(NetworkListener listener)
{
this.listener = listener;
this.context = context;
cancelList = new ArrayList<>();
}

public static NetworkManager getInstance(NetworkListener listener, Context context){
public static NetworkManager getInstance(NetworkListener listener){
if (instance == null)
instance = new NetworkManager(listener,context);
instance = new NetworkManager(listener);
if(listener != null && listener != instance.getListener())
instance.setListener(listener);
return instance;
@@ -77,12 +72,12 @@ public class NetworkManager {
this.listener = listener;
}

public void browseBinaries(){
public void browseBinaries(final Context context){
if(isConnectedToInternet(context)) {
new Thread(new Runnable() {
@Override
public void run() {
getBinariesList();
getBinariesList(context);
}
}).start();
}
@@ -90,20 +85,20 @@ public class NetworkManager {
listener.onError(context.getString(R.string.network_error));
}

public void retrieveBinary(final Server server, final Binary binary, final DownloadListener downloadListener){
public void retrieveBinary(final Context context, final Server server, final Binary binary, final DownloadListener downloadListener){
if(isConnectedToInternet(context)){
new Thread(new Runnable() {
@Override
public void run() {
getBinary(server, binary,downloadListener);
getBinary(context,server, binary,downloadListener);
}
}).start();
}
else
this.listener.onError(context.getString(R.string.network_error));
downloadListener.downloadError(server,binary,context.getString(R.string.network_error));
}

private void getBinariesList(){
private void getBinariesList(Context context){
List<List<Server>> servers = DatabaseManager.getInstance(context).getServers(context);
List<Server> serversAvailable = new ArrayList<>();
for(Server server : servers.get(0)){
@@ -170,10 +165,9 @@ public class NetworkManager {
return b;
}

private void getBinary(Server server, Binary binary, DownloadListener downloadListener){
private void getBinary(Context context, Server server, Binary binary, DownloadListener downloadListener){
try {
URL url = new URL(server.url.toString() + binary.path + binary.filename);
downloadInProgress = true;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
InputStream stream = connection.getInputStream();
@@ -189,15 +183,6 @@ public class NetworkManager {
long totalSizeReaded = 0;
int sizeReaded ;
while((sizeReaded = stream.read(data)) != -1){
if(cancelList.contains(binary)) {
downloadInProgress = false;
downloadListener.downloadCanceled(server,binary);
stream.close();
output.close();
cleanupFile(server,binary);
cancelList.remove(binary);
return;
}
totalSizeReaded += sizeReaded;
if(fileLength > 0){
downloadListener.progress(server,binary, (int) totalSizeReaded * 100 / fileLength);
@@ -205,22 +190,20 @@ public class NetworkManager {
output.write(data,0,sizeReaded);
}
output.close();
if(checkFileSha1(server,binary))
if(checkFileSha1(context,server,binary))
downloadListener.downloadSuccessful(server,binary);
else
downloadListener.downloadError(server,binary,context.getString(R.string.sha1_error));
}
}catch (IOException e) {
e.printStackTrace();
cleanupFile(server,binary);
cleanupFile(context,server,binary);
DatabaseManager.getInstance(context).serverFail(server);
downloadListener.downloadError(server,binary, context.getString(R.string.unknown_error));
}
downloadInProgress = false;
cancelList.remove(binary);
}

private boolean checkFileSha1(Server server, Binary binary){
private boolean checkFileSha1(Context context, Server server, Binary binary){
String sha1 = "";
try {
File f = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename);
@@ -241,16 +224,11 @@ public class NetworkManager {
return sha1.compareToIgnoreCase(binary.sha1) == 0;
}

private boolean cleanupFile(Server server, Binary binary){
private boolean cleanupFile(Context context, Server server, Binary binary){
File f = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename);
return f.delete();
}

public void cancelDownload(Binary binary){
if(downloadInProgress)
cancelList.add(binary);
}

private boolean isConnectedToInternet(Context context)
{
//verify the connectivity

+ 51
- 64
app/src/main/res/layout/binary_item.xml View File

@@ -2,84 +2,71 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="60dp">
android:layout_height="48dp"
>


<RelativeLayout
android:id="@+id/binary_info"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:orientation="horizontal"
android:layout_toLeftOf="@+id/delete_download"
android:layout_toStartOf="@+id/delete_download"
>
<LinearLayout
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/binary_version"
android:id="@+id/binary_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:gravity="center"
>

<ImageView
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:id="@+id/download"
android:src="@drawable/download"
android:layout_width="16dp"
android:layout_height="16dp" />
<TextView
android:layout_toRightOf="@+id/download"
android:layout_toLeftOf="@+id/binary_version"
android:id="@+id/binary_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView