diff --git a/app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java b/app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java index 9e8138d..b1d9bce 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java +++ b/app/src/main/java/fr/mobdev/lpcprog/activity/MainActivity.java @@ -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,15 +110,70 @@ 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); } } - },filter); + }; 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(); diff --git a/app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java b/app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java index 262f45e..1ccd333 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java +++ b/app/src/main/java/fr/mobdev/lpcprog/adapters/BinariesAdapter.java @@ -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 servers; private int count; - private Activity activity; - private FlashListener listener; + private BrowseRepositoryFragment.BrowseRepositoryListener listener; - public BinariesAdapter(List servers, Activity activity, FlashListener listener) { + public BinariesAdapter(List servers, BrowseRepositoryFragment.BrowseRepositoryListener listener) { this.servers = servers; - this.activity = activity; this.listener = listener; countElements(); } @@ -72,7 +63,14 @@ public class BinariesAdapter extends RecyclerView.Adapter { + BrowseDeviceFragment.BrowseDeviceListener listener; + private List devices; - public DeviceAdapter(List devices){ + public DeviceAdapter(List 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 { 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)); } diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java index 8eb012b..4b55015 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/AddPartFragment.java @@ -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; diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/USBListFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java similarity index 50% rename from app/src/main/java/fr/mobdev/lpcprog/fragment/USBListFragment.java rename to app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java index 75f2ab2..2680dad 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/fragment/USBListFragment.java +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseDeviceFragment.java @@ -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 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; + public void setBrowseDeviceListener(BrowseDeviceListener browseDeviceListener) { + listener = browseDeviceListener; } - @Override - public void onDetach() { - super.onDetach(); - listener = null; - } - - - private void onItemClick(int pos){ - if(devices.size()>pos){ - listener.onUSBDeviceClick(devices.get(pos)); - } - } - - public interface USBInteractionListener { - void onUSBDeviceClick(USBDevice device); + public interface BrowseDeviceListener { + void onDeviceClick(USBDevice device); } } diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java new file mode 100644 index 0000000..16a2e3f --- /dev/null +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/BrowseRepositoryFragment.java @@ -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 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> 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); + } + +} diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java deleted file mode 100644 index cd4cd57..0000000 --- a/app/src/main/java/fr/mobdev/lpcprog/fragment/DeviceFragment.java +++ /dev/null @@ -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 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 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> 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(); - } -} diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java new file mode 100644 index 0000000..aa9defd --- /dev/null +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/FlashFragment.java @@ -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; + } +} diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java new file mode 100644 index 0000000..236701c --- /dev/null +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/HomeFragment.java @@ -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(); + } + +} diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java index a4e5824..7a97f0a 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/PartsFragment.java @@ -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; diff --git a/app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java b/app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java index 06768be..8a35e62 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java +++ b/app/src/main/java/fr/mobdev/lpcprog/fragment/ServersFragment.java @@ -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; diff --git a/app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java b/app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java index a2c2e76..b55e50f 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java +++ b/app/src/main/java/fr/mobdev/lpcprog/listener/DownloadListener.java @@ -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); diff --git a/app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java b/app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java index 82e8d72..1310136 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java +++ b/app/src/main/java/fr/mobdev/lpcprog/listener/FlashListener.java @@ -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); } diff --git a/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java b/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java index 9a0ab6f..711d25c 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java +++ b/app/src/main/java/fr/mobdev/lpcprog/managers/IspManager.java @@ -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); } diff --git a/app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java b/app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java index 7b41e98..a5b52b6 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java +++ b/app/src/main/java/fr/mobdev/lpcprog/managers/NetworkManager.java @@ -48,20 +48,15 @@ public class NetworkManager { private NetworkListener listener; private static NetworkManager instance; - private Context context; - private boolean downloadInProgress; - private List 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> servers = DatabaseManager.getInstance(context).getServers(context); List 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 diff --git a/app/src/main/res/layout/binary_item.xml b/app/src/main/res/layout/binary_item.xml index d846162..efc5579 100644 --- a/app/src/main/res/layout/binary_item.xml +++ b/app/src/main/res/layout/binary_item.xml @@ -2,84 +2,71 @@ + android:layout_height="48dp" + > + - + + android:layout_height="match_parent" + android:layout_marginLeft="10dp" + android:layout_marginStart="10dp" + android:gravity="center" + > + + + + - - - - - - - - - + android:layout_height="match_parent" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/device.xml b/app/src/main/res/layout/device.xml deleted file mode 100644 index e8228fb..0000000 --- a/app/src/main/res/layout/device.xml +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/device_item.xml b/app/src/main/res/layout/device_item.xml index 2be5fe7..c865234 100644 --- a/app/src/main/res/layout/device_item.xml +++ b/app/src/main/res/layout/device_item.xml @@ -3,7 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="100dp"> + android:layout_height="60dp"> + + + \ No newline at end of file diff --git a/app/src/main/res/layout/flash.xml b/app/src/main/res/layout/flash.xml new file mode 100644 index 0000000..60acfeb --- /dev/null +++ b/app/src/main/res/layout/flash.xml @@ -0,0 +1,164 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +