From dd94f30c8a8db42e1c32e71418d7a83ea343e943 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Mon, 7 Sep 2015 00:07:39 -0700 Subject: [PATCH] Implemented the context menu for PairingFragment --- res/layout/custom_ip_list.xml | 1 - res/menu/new_pairing.xml | 16 ++-- res/values/strings.xml | 3 +- .../NewUserInterface/MaterialActivity.java | 46 ++++++----- .../NewUserInterface/PairingFragment.java | 56 +++++++++++-- .../UserInterface/CustomDevicesActivity.java | 82 +++++++++++-------- 6 files changed, 133 insertions(+), 71 deletions(-) diff --git a/res/layout/custom_ip_list.xml b/res/layout/custom_ip_list.xml index 80d19379..a433ed57 100644 --- a/res/layout/custom_ip_list.xml +++ b/res/layout/custom_ip_list.xml @@ -30,7 +30,6 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add_host" - android:onClick="addNewIp" android:id="@android:id/button1"/> diff --git a/res/menu/new_pairing.xml b/res/menu/new_pairing.xml index 4f0b81e4..ffc8c144 100644 --- a/res/menu/new_pairing.xml +++ b/res/menu/new_pairing.xml @@ -2,11 +2,11 @@ xmlns:kdeconnect="http://schemas.android.com/apk/res-auto/android"> Send files KDE Connect Devices - "Other devices running KDE Connect in your same network should appear here + Other devices running KDE Connect in your same network should appear here. Device paired Rename device Rename + Refresh diff --git a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java index 798112ce..2dce6d46 100644 --- a/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java +++ b/src/org/kde/kdeconnect/NewUserInterface/MaterialActivity.java @@ -64,32 +64,13 @@ public class MaterialActivity extends AppCompatActivity { mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); String deviceName = DeviceHelper.getDeviceName(this); - final TextView nameView = (TextView) mDrawerLayout.findViewById(R.id.device_name); + TextView nameView = (TextView) mDrawerLayout.findViewById(R.id.device_name); nameView.setText(deviceName); nameView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - final EditText deviceNameEdit = new EditText(MaterialActivity.this); - String deviceName = DeviceHelper.getDeviceName(MaterialActivity.this); - deviceNameEdit.setText(deviceName); - new AlertDialog.Builder(MaterialActivity.this) - .setView(deviceNameEdit) - .setPositiveButton(R.string.device_rename_confirm, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String deviceName = deviceNameEdit.getText().toString(); - DeviceHelper.setDeviceName(MaterialActivity.this, deviceName); - nameView.setText(deviceName); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }) - .setTitle(R.string.device_rename_title) - .show(); + renameDevice(); } }); @@ -263,6 +244,29 @@ public class MaterialActivity extends AppCompatActivity { default: super.onActivityResult(requestCode, resultCode, data); } + } + void renameDevice() { + final TextView nameView = (TextView) mDrawerLayout.findViewById(R.id.device_name); + final EditText deviceNameEdit = new EditText(MaterialActivity.this); + String deviceName = DeviceHelper.getDeviceName(MaterialActivity.this); + deviceNameEdit.setText(deviceName); + new AlertDialog.Builder(MaterialActivity.this) + .setView(deviceNameEdit) + .setPositiveButton(R.string.device_rename_confirm, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String deviceName = deviceNameEdit.getText().toString(); + DeviceHelper.setDeviceName(MaterialActivity.this, deviceName); + nameView.setText(deviceName); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .setTitle(R.string.device_rename_title) + .show(); } } diff --git a/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java b/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java index 4ef87a33..d1f21f3d 100644 --- a/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java +++ b/src/org/kde/kdeconnect/NewUserInterface/PairingFragment.java @@ -51,19 +51,20 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb private static final int RESULT_PAIRING_SUCCESFUL = Activity.RESULT_FIRST_USER; - /** - * The fragment argument representing the section number for this - * fragment. - */ private View rootView; private MaterialActivity mActivity; + private MenuItem menuProgress; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mActivity.getSupportActionBar().setTitle(R.string.pairing_title); + + + setHasOptionsMenu(true); + rootView = inflater.inflate(R.layout.activity_main, container, false); TextView text = new TextView(inflater.getContext()); @@ -170,7 +171,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - switch(requestCode) { + switch (requestCode) { case RESULT_PAIRING_SUCCESFUL: if (resultCode == 1) { String deviceId = data.getStringExtra("deviceId"); @@ -181,4 +182,49 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb } } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.new_pairing, menu); + menuProgress = menu.findItem(R.id.menu_progress); + } + + @Override + public boolean onOptionsItemSelected(final MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_refresh: + updateComputerList(); + BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + service.onNetworkChange(); + } + }); + menuProgress.setVisible(true); + new Thread(new Runnable() { + @Override + public void run() { + try { Thread.sleep(1500); } catch (InterruptedException e) { } + mActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + item.setVisible(true); + } + }); + } + }).start(); + break; + case R.id.menu_rename: + mActivity.renameDevice(); + break; + case R.id.menu_custom_device_list: + startActivity(new Intent(mActivity, CustomDevicesActivity.class)); + break; + default: + break; + } + return true; + } + + + } diff --git a/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java b/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java index 7db8d1b4..b06b2287 100644 --- a/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java @@ -21,16 +21,17 @@ package org.kde.kdeconnect.UserInterface; import android.app.AlertDialog; -import android.app.ListActivity; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; @@ -40,12 +41,14 @@ import org.kde.kdeconnect_tp.R; import java.util.ArrayList; -public class CustomDevicesActivity extends ListActivity { +public class CustomDevicesActivity extends ActionBarActivity { private static final String LOG_ID = "CustomDevicesActivity"; public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference"; private static final String IP_DELIM = ","; + private ListView list; + private ArrayList ipAddressList = new ArrayList<>(); @Override @@ -53,14 +56,24 @@ public class CustomDevicesActivity extends ListActivity { super.onCreate(savedInstanceState); initializeDeviceList(this); setContentView(R.layout.custom_ip_list); - setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList)); + list = (ListView)findViewById(android.R.id.list); + list.setOnItemClickListener(onClickListener); + + list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList)); + + findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + addNewDevice(); + } + }); EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext); ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEND) { - addNewIp(); + addNewDevice(); return true; } return false; @@ -68,36 +81,38 @@ public class CustomDevicesActivity extends ListActivity { }); } - @Override - public void onListItemClick(ListView l, View v, final int position, final long id) { - Log.i(LOG_ID, "Item clicked pos: " + position + " id: " + id); - // remove touched item after confirmation - DialogInterface.OnClickListener confirmationListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - switch (which) { - case DialogInterface.BUTTON_POSITIVE: - ipAddressList.remove(position); - Log.i(LOG_ID, "Removed item pos: "+position+" id: "+id); - saveList(); - break; - case DialogInterface.BUTTON_NEGATIVE: - break; + private AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, final int position, final long id) { + Log.i(LOG_ID, "Item clicked pos: " + position + " id: " + id); + // remove touched item after confirmation + DialogInterface.OnClickListener confirmationListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: + ipAddressList.remove(position); + Log.i(LOG_ID, "Removed item pos: " + position + " id: " + id); + saveList(); + break; + case DialogInterface.BUTTON_NEGATIVE: + break; + } } - } - }; - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("Delete "+ipAddressList.get(position)+" ?"); - builder.setPositiveButton("Yes", confirmationListener); - builder.setNegativeButton("No", confirmationListener); - builder.show(); - ((ArrayAdapter)getListAdapter()).notifyDataSetChanged(); - } + }; + AlertDialog.Builder builder = new AlertDialog.Builder(CustomDevicesActivity.this); + builder.setMessage("Delete " + ipAddressList.get(position) + " ?"); + builder.setPositiveButton("Yes", confirmationListener); + builder.setNegativeButton("No", confirmationListener); + builder.show(); + ((ArrayAdapter) list.getAdapter()).notifyDataSetChanged(); + } + }; - private void addNewIp() { + private void addNewDevice() { EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext); String enteredText = ipEntryBox.getText().toString().trim(); - if (!enteredText.equals("")) { + if (!enteredText.isEmpty()) { // don't add empty string (after trimming) ipAddressList.add(enteredText); } @@ -121,9 +136,9 @@ public class CustomDevicesActivity extends ListActivity { if (!ipAddressList.isEmpty()) { serialized = serializeIpList(ipAddressList); } - PreferenceManager.getDefaultSharedPreferences(this).edit().putString( + PreferenceManager.getDefaultSharedPreferences(CustomDevicesActivity.this).edit().putString( KEY_CUSTOM_DEVLIST_PREFERENCE, serialized).commit(); - ((ArrayAdapter)getListAdapter()).notifyDataSetChanged(); + ((ArrayAdapter)list.getAdapter()).notifyDataSetChanged(); } @@ -150,8 +165,7 @@ public class CustomDevicesActivity extends ListActivity { private void initializeDeviceList(Context context){ String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString( - KEY_CUSTOM_DEVLIST_PREFERENCE, - ""); + KEY_CUSTOM_DEVLIST_PREFERENCE, ""); if(deviceListPrefs.isEmpty()){ Log.i(LOG_ID, "Initialising empty custom device list"); PreferenceManager.getDefaultSharedPreferences(context).edit().putString(