2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Implemented the context menu for PairingFragment

This commit is contained in:
Albert Vaca
2015-09-07 00:07:39 -07:00
parent e2081d3892
commit dd94f30c8a
6 changed files with 133 additions and 71 deletions

View File

@@ -30,7 +30,6 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/add_host" android:text="@string/add_host"
android:onClick="addNewIp"
android:id="@android:id/button1"/> android:id="@android:id/button1"/>

View File

@@ -2,11 +2,11 @@
xmlns:kdeconnect="http://schemas.android.com/apk/res-auto/android"> xmlns:kdeconnect="http://schemas.android.com/apk/res-auto/android">
<item <item
android:id="@+id/menu_refresh" android:id="@+id/menu_refresh"
android:icon="@drawable/ic_action_refresh" android:icon="@drawable/ic_action_refresh"
android:orderInCategory="200" android:orderInCategory="200"
kdeconnect:showAsAction="ifRoom" kdeconnect:showAsAction="never"
android:title="@string/reconnect" android:title="@string/refresh"
/> />
<item <item
@@ -18,16 +18,14 @@
/> />
<item <item
android:id="@+id/menu_settings" android:id="@+id/menu_rename"
android:icon="@drawable/ic_action_settings"
android:orderInCategory="300" android:orderInCategory="300"
android:title="@string/settings" android:title="@string/device_rename_title"
kdeconnect:showAsAction="never" kdeconnect:showAsAction="never"
/> />
<item <item
android:id="@+id/menu_custom_device_list" android:id="@+id/menu_custom_device_list"
android:icon="@drawable/ic_action_settings"
android:orderInCategory="900" android:orderInCategory="900"
android:title="@string/custom_device_list" android:title="@string/custom_device_list"
kdeconnect:showAsAction="never" kdeconnect:showAsAction="never"

View File

@@ -146,9 +146,10 @@
<string name="send_files">Send files</string> <string name="send_files">Send files</string>
<string name="pairing_title">KDE Connect Devices</string> <string name="pairing_title">KDE Connect Devices</string>
<string name="pairing_description">"Other devices running KDE Connect in your same network should appear here</string> <string name="pairing_description">Other devices running KDE Connect in your same network should appear here.</string>
<string name="device_paired">Device paired</string> <string name="device_paired">Device paired</string>
<string name="device_rename_title">Rename device</string> <string name="device_rename_title">Rename device</string>
<string name="device_rename_confirm">Rename</string> <string name="device_rename_confirm">Rename</string>
<string name="refresh">Refresh</string>
</resources> </resources>

View File

@@ -64,32 +64,13 @@ public class MaterialActivity extends AppCompatActivity {
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
String deviceName = DeviceHelper.getDeviceName(this); 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.setText(deviceName);
nameView.setOnClickListener(new View.OnClickListener() { nameView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final EditText deviceNameEdit = new EditText(MaterialActivity.this); renameDevice();
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();
} }
}); });
@@ -263,6 +244,29 @@ public class MaterialActivity extends AppCompatActivity {
default: default:
super.onActivityResult(requestCode, resultCode, data); 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();
} }
} }

View File

@@ -51,19 +51,20 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
private static final int RESULT_PAIRING_SUCCESFUL = Activity.RESULT_FIRST_USER; 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 View rootView;
private MaterialActivity mActivity; private MaterialActivity mActivity;
private MenuItem menuProgress;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mActivity.getSupportActionBar().setTitle(R.string.pairing_title); mActivity.getSupportActionBar().setTitle(R.string.pairing_title);
setHasOptionsMenu(true);
rootView = inflater.inflate(R.layout.activity_main, container, false); rootView = inflater.inflate(R.layout.activity_main, container, false);
TextView text = new TextView(inflater.getContext()); TextView text = new TextView(inflater.getContext());
@@ -170,7 +171,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) { switch (requestCode) {
case RESULT_PAIRING_SUCCESFUL: case RESULT_PAIRING_SUCCESFUL:
if (resultCode == 1) { if (resultCode == 1) {
String deviceId = data.getStringExtra("deviceId"); 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;
}
} }

View File

@@ -21,16 +21,17 @@
package org.kde.kdeconnect.UserInterface; package org.kde.kdeconnect.UserInterface;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
@@ -40,12 +41,14 @@ import org.kde.kdeconnect_tp.R;
import java.util.ArrayList; import java.util.ArrayList;
public class CustomDevicesActivity extends ListActivity { public class CustomDevicesActivity extends ActionBarActivity {
private static final String LOG_ID = "CustomDevicesActivity"; private static final String LOG_ID = "CustomDevicesActivity";
public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference"; public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference";
private static final String IP_DELIM = ","; private static final String IP_DELIM = ",";
private ListView list;
private ArrayList<String> ipAddressList = new ArrayList<>(); private ArrayList<String> ipAddressList = new ArrayList<>();
@Override @Override
@@ -53,14 +56,24 @@ public class CustomDevicesActivity extends ListActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
initializeDeviceList(this); initializeDeviceList(this);
setContentView(R.layout.custom_ip_list); 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); EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext);
ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() { ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) { if (actionId == EditorInfo.IME_ACTION_SEND) {
addNewIp(); addNewDevice();
return true; return true;
} }
return false; return false;
@@ -68,36 +81,38 @@ public class CustomDevicesActivity extends ListActivity {
}); });
} }
@Override private AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() {
public void onListItemClick(ListView l, View v, final int position, final long id) { @Override
Log.i(LOG_ID, "Item clicked pos: " + position + " id: " + id); public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
// remove touched item after confirmation Log.i(LOG_ID, "Item clicked pos: " + position + " id: " + id);
DialogInterface.OnClickListener confirmationListener = new DialogInterface.OnClickListener() { // remove touched item after confirmation
@Override DialogInterface.OnClickListener confirmationListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { @Override
switch (which) { public void onClick(DialogInterface dialog, int which) {
case DialogInterface.BUTTON_POSITIVE: switch (which) {
ipAddressList.remove(position); case DialogInterface.BUTTON_POSITIVE:
Log.i(LOG_ID, "Removed item pos: "+position+" id: "+id); ipAddressList.remove(position);
saveList(); Log.i(LOG_ID, "Removed item pos: " + position + " id: " + id);
break; saveList();
case DialogInterface.BUTTON_NEGATIVE: break;
break; case DialogInterface.BUTTON_NEGATIVE:
break;
}
} }
} };
}; AlertDialog.Builder builder = new AlertDialog.Builder(CustomDevicesActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Delete " + ipAddressList.get(position) + " ?");
builder.setMessage("Delete "+ipAddressList.get(position)+" ?"); builder.setPositiveButton("Yes", confirmationListener);
builder.setPositiveButton("Yes", confirmationListener); builder.setNegativeButton("No", confirmationListener);
builder.setNegativeButton("No", confirmationListener); builder.show();
builder.show(); ((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
((ArrayAdapter)getListAdapter()).notifyDataSetChanged(); }
} };
private void addNewIp() { private void addNewDevice() {
EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext); EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext);
String enteredText = ipEntryBox.getText().toString().trim(); String enteredText = ipEntryBox.getText().toString().trim();
if (!enteredText.equals("")) { if (!enteredText.isEmpty()) {
// don't add empty string (after trimming) // don't add empty string (after trimming)
ipAddressList.add(enteredText); ipAddressList.add(enteredText);
} }
@@ -121,9 +136,9 @@ public class CustomDevicesActivity extends ListActivity {
if (!ipAddressList.isEmpty()) { if (!ipAddressList.isEmpty()) {
serialized = serializeIpList(ipAddressList); serialized = serializeIpList(ipAddressList);
} }
PreferenceManager.getDefaultSharedPreferences(this).edit().putString( PreferenceManager.getDefaultSharedPreferences(CustomDevicesActivity.this).edit().putString(
KEY_CUSTOM_DEVLIST_PREFERENCE, serialized).commit(); 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){ private void initializeDeviceList(Context context){
String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString( String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString(
KEY_CUSTOM_DEVLIST_PREFERENCE, KEY_CUSTOM_DEVLIST_PREFERENCE, "");
"");
if(deviceListPrefs.isEmpty()){ if(deviceListPrefs.isEmpty()){
Log.i(LOG_ID, "Initialising empty custom device list"); Log.i(LOG_ID, "Initialising empty custom device list");
PreferenceManager.getDefaultSharedPreferences(context).edit().putString( PreferenceManager.getDefaultSharedPreferences(context).edit().putString(