import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class FTPPasswordFragment extends Fragment {
String retStr = "";
ArrayList<XMLListModel> myCustomersList = null; //List everyone in the customer database
ArrayList<XMLListModel> myDistributorList = null; //List of distributors
ArrayList<XMLListModel> myClients = null; //List the clients of distributors
FTPClientManipulate myFTPClient = null;
// LoadingScreenActivity Loading;
//private Context context;
//private ProcessDialog LoadingBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.ftp_password);
}
////////////////////////////////////////////////////////////////////////////
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myFTPClient = new FTPClientManipulate();
if(myFTPClient.ftpConnect() && myFTPClient.ftpDownload(FTPPathNameFinder.getListPath(), new FTPPathNameFinder().getListFileName(FTPPathNameFinder.customersListName))) {
myCustomersList = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Customer", "Name", "File")), myFTPClient.getStringResult());
myDistributorList = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Dist", "ID", "Pwd")), myFTPClient.getStringResult());
myFTPClient.ftpDisconnect();
if(myCustomersList == null) {
Toast.makeText(getActivity(), "File Not Available!", Toast.LENGTH_LONG).show();
System.exit(0);
}
} else {
myFTPClient.ftpDisconnect();
//Toast.makeText(getActivity(), "No FTP Connection!", Toast.LENGTH_LONG).show();
//String ttl = ;
new AlertDialog.Builder(getActivity())
.setTitle(Html.fromHtml("<font color='#FFFF00'>" + "Network Error!" + "</font>"))
.setMessage("Please Check the Internet Connection...")
.setIcon(R.drawable.traffic_lights_red)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
System.exit(0);
}}).show();
}
//Toast.makeText(getActivity(), myFTPClient.getStringResult(), Toast.LENGTH_LONG).show();
Button ok = (Button) getView().findViewById(R.id.ftp_password_ok);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
// TODO Auto-generated method stub
AlertDialog LoadingScreen = new AlertDialog.Builder(getActivity())
.setTitle(Html.fromHtml("Loading Files"))
.setMessage("Please Wait ...")
.setIcon(R.drawable.traffic_lights_red)
.create();
LoadingScreen.show(); //Throw the Alert Dialog box
//Loading Screen seems to be shown just before displaying the files
//and not straigh way when the click button is pressed
//Need to set priority of displaying this message
// Not sure if it is AsyncTask or something else.
passwordOK(); //Throw to check password and load list.
}
});
Button cancel = (Button) getView().findViewById(R.id.ftp_password_cancel);
cancel.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
// TODO Auto-generated method stub
System.exit(0);
}
});
}
////////////////////////////////////////////////////////////////////////////
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ftp_password_view, container, false);
return view;
}
////////////////////////////////////////////////////////////////////////////
//
public void passwordOK() {
Boolean userNameValid = false;
EditText passwordInput = (EditText) getView().findViewById(R.id.password);
EditText userNameInput = (EditText) getView().findViewById(R.id.userName);
//FTPClientManipulate myFTPClient = new FTPClientManipulate();
// Intent LoadingIntent = new Intent(getActivity(), LoadingScreenActivity.class);
// this.startActivity(LoadingIntent);
if(myFTPClient.ftpConnect()) {
if(userNameInput.getText().toString().equals(FTPPathNameFinder.administratorName)) {
userNameValid = true;
if(myFTPClient.ftpDownload(FTPPathNameFinder.getPasswordPath(), new FTPPathNameFinder().getPasswordFileName(FTPPathNameFinder.administratorName))) {
if(myFTPClient.getStringResult().equals(passwordInput.getText().toString())) {
//((FTPPassword)getActivity()).switchToCustomersList(myCustomersList);
SignInfo.setRemoteAccessFlag(true);
Intent myIntent = new Intent(getActivity(), CustomersList.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(DevicesListFragment.listName, myCustomersList);
myIntent.putExtras(bundle);
getActivity().startActivity(myIntent);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Login Failed!", Toast.LENGTH_LONG).show();
}
} else {
}
} else {
//This is an ordinary customer
for(XMLListModel customer: myCustomersList) {
if(customer.getName().equals(userNameInput.getText().toString())) {
//((FTPPassword)getActivity()).toast("Valid: " + customer.getName());
userNameValid = true;
if(myFTPClient.ftpDownload(FTPPathNameFinder.getPasswordPath(),new FTPPathNameFinder().getPasswordFileName(customer.getSecondTag()))) {
//((FTPPassword)getActivity()).toast("Password Valid: " + customer.getName());
if(myFTPClient.getStringResult().equals(passwordInput.getText().toString())) {
if(myFTPClient.ftpDownload(FTPPathNameFinder.getListPath(), new FTPPathNameFinder().getListFileName(customer.getSecondTag()))) {
//ret = new FileManipulate(getApplicationContext()).ReadFileAsString(tmpFileName);
//((FTPPassword)getActivity()).toast("Devices: " + myFTPClient.getStringResult());
try {
ArrayList<XMLListModel> myData = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Device", "Name", "ID")), myFTPClient.getStringResult());
//ArrayList<XMLListModel> myData = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Customer", "Name", "File")), myFTPClient.output);
myFTPClient.ftpDisconnect();
try {
if(myData != null){
//((FTPPassword)getActivity()).switchToDevicesList(myData);
SignInfo.setRemoteAccessFlag(true);
Intent myIntent = new Intent(getActivity(), DevicesList.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(DevicesListFragment.listName, myData);
myIntent.putExtras(bundle);
getActivity().startActivity(myIntent);
getActivity().finish();
} else {
}
} catch(Exception e) {
}
} catch (Exception e) {
Toast.makeText(getActivity(), "XML Exception", Toast.LENGTH_LONG).show();
userNameInput.setText("");
passwordInput.setText("");
}
} else {
Toast.makeText(getActivity(), "No Network Connection!", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getActivity(), "Password Error!", Toast.LENGTH_LONG).show();
passwordInput.setText("");
}
} else {
Toast.makeText(getActivity(), "No Network Connection!", Toast.LENGTH_LONG).show();
}
}
}
//Check the Distributor List
for(XMLListModel Distributor: myDistributorList) {
if(Distributor.getName().equals(userNameInput.getText().toString())) {
//We have a vaild Distributor
userNameValid = true;
if(myFTPClient.ftpDownload(FTPPathNameFinder.getPasswordPath(),new FTPPathNameFinder().getPasswordFileName(Distributor.getSecondTag()))) {
//Download the Password file successful
//((FTPPassword)getActivity()).toast("Password Valid: " + customer.getName());
if(myFTPClient.getStringResult().equals(passwordInput.getText().toString())) //Check the password to see if it matches
{
//Distributor password matches
//Need to filter from the customer files who the distributor is
ArrayList<XMLListModel> myClients = new ArrayList<XMLListModel>();
for (XMLListModel customer: myCustomersList){
if(myFTPClient.ftpDownload(FTPPathNameFinder.getListPath(), new FTPPathNameFinder().getListFileName(customer.getSecondTag()))) {
//Download the customers xml file which lists the signs and current distributor
//ret = new FileManipulate(getApplicationContext()).ReadFileAsString(tmpFileName);
//((FTPPassword)getActivity()).toast("Devices: " + myFTPClient.getStringResult());
try {
//extract the customers name
String client = customer.getName().toString();
//extract the dist element from the XML file
String ReadDist = myFTPClient.getStringResult().substring(myFTPClient.getStringResult().indexOf("<Dist>")+6,myFTPClient.getStringResult().indexOf("</Dist>"));
//Compare the distributors name against the dist element pulled from file
if(Distributor.getName().equals(ReadDist) || ReadDist.equals("All"))
{
//If they are the same then we want to add the customers name to the list to display
myClients.add(customer);
// ArrayList<XMLListModel> myClients = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Customer", "Name","File")), customer.toString());
//ArrayList<XMLListModel> myData = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Customer", "Name", "File")), myFTPClient.output);
}
//ArrayList<XMLListModel> myClients = new XMLStringToList()
//myDistributorList = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Dist", "ID", "Pwd")), myFTPClient.getStringResult());
// ArrayList<XMLListModel> myData = new XMLStringToList().getList(new ArrayList<String>(Arrays.asList("Customer", "Name", "File")), myFTPClient.output);
//myFTPClient.ftpDisconnect();
} catch (Exception e) {
Toast.makeText(getActivity(), "XML Exception", Toast.LENGTH_LONG).show();
userNameInput.setText("");
passwordInput.setText("");
}
} else {
Toast.makeText(getActivity(), "No Network Connection!", Toast.LENGTH_LONG).show();
}
//Need to implement a loading screen
// getActivity().finish();
} //END FOR LOOP
// getActivity().finish());
myFTPClient.ftpDisconnect();
//Now try and display the list
try {
if(myClients != null){
//((FTPPassword)getActivity()).switchToDevicesList(myData);
SignInfo.setRemoteAccessFlag(true);
Intent myIntent = new Intent(getActivity(), CustomersList.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(DevicesListFragment.listName, myClients);
myIntent.putExtras(bundle);
getActivity().startActivity(myIntent);
getActivity().finish();
} else {
}
} catch(Exception e) {
}
} else {
Toast.makeText(getActivity(), "Password Error!", Toast.LENGTH_LONG).show();
passwordInput.setText("");
}
} else {
Toast.makeText(getActivity(), "No Network Connection!", Toast.LENGTH_LONG).show();
}
}
}
if(!userNameValid) {
Toast.makeText(getActivity(), "Username Error!", Toast.LENGTH_LONG).show();
userNameInput.setText("");
passwordInput.setText("");
userNameInput.requestFocus();
}
userNameValid = false;
}
} else {
Toast.makeText(getActivity(), "No Network Connection!", Toast.LENGTH_LONG).show();
}
myFTPClient.ftpDisconnect();
}
////////////////////////////////////////////////////////////////////////////
}