2014-11-20 23:51:04 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Achilleas Koutsou <achilleas.k@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-11-20 23:47:43 -08:00
|
|
|
package org.kde.kdeconnect.UserInterface;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2016-09-29 13:20:43 +02:00
|
|
|
import android.os.Build;
|
2014-11-20 23:47:43 -08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
2017-07-25 18:12:18 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2018-03-04 13:19:57 +01:00
|
|
|
import android.text.TextUtils;
|
2014-11-20 23:47:43 -08:00
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2015-09-07 00:07:39 -07:00
|
|
|
import android.widget.AdapterView;
|
2014-11-20 23:47:43 -08:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-09-09 12:34:42 -07:00
|
|
|
import org.kde.kdeconnect.BackgroundService;
|
2014-11-20 23:47:43 -08:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-03-04 13:19:57 +01:00
|
|
|
import java.util.Collections;
|
2014-11-20 23:47:43 -08:00
|
|
|
|
2017-07-25 18:12:18 +02:00
|
|
|
public class CustomDevicesActivity extends AppCompatActivity {
|
2014-11-20 23:47:43 -08:00
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference";
|
2014-11-20 23:47:43 -08:00
|
|
|
private static final String IP_DELIM = ",";
|
|
|
|
|
2015-09-07 00:07:39 -07:00
|
|
|
private ListView list;
|
|
|
|
|
2015-08-10 00:26:58 -07:00
|
|
|
private ArrayList<String> ipAddressList = new ArrayList<>();
|
2014-11-20 23:47:43 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
initializeDeviceList(this);
|
Add a dark theme
Summary:
BUG: 375376
This revision adds dark mode support to the app ( T7044 ). It does so by injecting
theme information into each activity, and making sure that all Views
define their colors by reference to theme attributes.
In order to make this work, all of the buttons with images (like the list
of available devices) now are tinted according to the theme.
While all versions of android support the theme, only devices running
Android ICS or higher will have a toggle button in the drawer.
Test Plan: Open all the screens, both with and without the dark theme on.
Reviewers: #kde_connect, mtijink, #vdg, nicolasfella
Reviewed By: #kde_connect, mtijink, nicolasfella
Subscribers: apol, ngraham, nicolasfella, mtijink
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D11694
2018-04-23 18:31:44 +02:00
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
2014-11-20 23:47:43 -08:00
|
|
|
setContentView(R.layout.custom_ip_list);
|
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
list = (ListView) findViewById(android.R.id.list);
|
2015-09-07 00:07:39 -07:00
|
|
|
list.setOnItemClickListener(onClickListener);
|
|
|
|
|
|
|
|
list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList));
|
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
findViewById(android.R.id.button1).setOnClickListener(v -> addNewDevice());
|
2014-11-20 23:47:43 -08:00
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
EditText ipEntryBox = (EditText) findViewById(R.id.ip_edittext);
|
2018-05-09 14:02:56 +02:00
|
|
|
ipEntryBox.setOnEditorActionListener((v, actionId, event) -> {
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_SEND) {
|
|
|
|
addNewDevice();
|
|
|
|
return true;
|
2014-11-20 23:47:43 -08:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
return false;
|
2014-11-20 23:47:43 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 13:20:43 +02:00
|
|
|
boolean dialogAlreadyShown = false;
|
2018-05-09 14:02:56 +02:00
|
|
|
private AdapterView.OnItemClickListener onClickListener = (parent, view, position, id) -> {
|
2016-09-29 13:20:43 +02:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
if (dialogAlreadyShown) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-29 13:20:43 +02:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
// remove touched item after confirmation
|
|
|
|
DialogInterface.OnClickListener confirmationListener = (dialog, which) -> {
|
|
|
|
switch (which) {
|
|
|
|
case DialogInterface.BUTTON_POSITIVE:
|
|
|
|
ipAddressList.remove(position);
|
|
|
|
saveList();
|
|
|
|
break;
|
|
|
|
case DialogInterface.BUTTON_NEGATIVE:
|
|
|
|
break;
|
2016-09-29 13:20:43 +02:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
};
|
2016-09-29 13:20:43 +02:00
|
|
|
|
2018-05-09 14:02:56 +02:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(CustomDevicesActivity.this);
|
|
|
|
builder.setMessage("Delete " + ipAddressList.get(position) + " ?");
|
|
|
|
builder.setPositiveButton("Yes", confirmationListener);
|
|
|
|
builder.setNegativeButton("No", confirmationListener);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { //DismissListener
|
|
|
|
dialogAlreadyShown = true;
|
|
|
|
builder.setOnDismissListener(dialog -> dialogAlreadyShown = false);
|
2015-09-07 00:07:39 -07:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
|
|
|
|
builder.show();
|
2015-09-07 00:07:39 -07:00
|
|
|
};
|
2014-11-20 23:47:43 -08:00
|
|
|
|
2015-09-07 00:07:39 -07:00
|
|
|
private void addNewDevice() {
|
2018-03-03 16:06:52 +01:00
|
|
|
EditText ipEntryBox = (EditText) findViewById(R.id.ip_edittext);
|
2014-11-20 23:47:43 -08:00
|
|
|
String enteredText = ipEntryBox.getText().toString().trim();
|
2015-09-07 00:07:39 -07:00
|
|
|
if (!enteredText.isEmpty()) {
|
2014-11-20 23:47:43 -08:00
|
|
|
// don't add empty string (after trimming)
|
|
|
|
ipAddressList.add(enteredText);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveList();
|
|
|
|
// clear entry box
|
|
|
|
ipEntryBox.setText("");
|
|
|
|
InputMethodManager inputManager = (InputMethodManager)
|
|
|
|
getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
2015-04-04 14:25:02 -07:00
|
|
|
View focus = getCurrentFocus();
|
2018-03-04 13:19:57 +01:00
|
|
|
if (focus != null && inputManager != null) {
|
2015-04-04 14:25:02 -07:00
|
|
|
inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
|
|
|
|
InputMethodManager.HIDE_NOT_ALWAYS);
|
|
|
|
}
|
2014-11-20 23:47:43 -08:00
|
|
|
}
|
|
|
|
|
2015-08-10 00:26:58 -07:00
|
|
|
private void saveList() {
|
2018-03-04 13:19:57 +01:00
|
|
|
String serialized = TextUtils.join(IP_DELIM, ipAddressList);
|
2015-09-07 00:07:39 -07:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(CustomDevicesActivity.this).edit().putString(
|
2018-03-04 13:19:57 +01:00
|
|
|
KEY_CUSTOM_DEVLIST_PREFERENCE, serialized).apply();
|
2018-03-03 16:06:52 +01:00
|
|
|
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
|
2014-11-20 23:47:43 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ArrayList<String> deserializeIpList(String serialized) {
|
2016-09-29 13:21:13 +02:00
|
|
|
ArrayList<String> ipList = new ArrayList<>();
|
2018-03-04 13:19:57 +01:00
|
|
|
Collections.addAll(ipList, serialized.split(IP_DELIM));
|
2016-09-29 13:21:13 +02:00
|
|
|
return ipList;
|
2014-11-20 23:47:43 -08:00
|
|
|
}
|
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
private void initializeDeviceList(Context context) {
|
2014-11-20 23:47:43 -08:00
|
|
|
String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString(
|
2015-09-07 00:07:39 -07:00
|
|
|
KEY_CUSTOM_DEVLIST_PREFERENCE, "");
|
2018-03-03 16:06:52 +01:00
|
|
|
if (deviceListPrefs.isEmpty()) {
|
2014-11-20 23:47:43 -08:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(
|
|
|
|
KEY_CUSTOM_DEVLIST_PREFERENCE,
|
2018-03-04 13:19:57 +01:00
|
|
|
deviceListPrefs).apply();
|
2014-11-20 23:47:43 -08:00
|
|
|
} else {
|
|
|
|
ipAddressList = deserializeIpList(deviceListPrefs);
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 12:34:42 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
BackgroundService.addGuiInUseCounter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
BackgroundService.removeGuiInUseCounter(this);
|
|
|
|
}
|
|
|
|
|
2014-11-20 23:47:43 -08:00
|
|
|
}
|