Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/UserInterface/CustomDevicesActivity.java
Show All 39 Lines | |||||
40 | import org.kde.kdeconnect.BackgroundService; | 40 | import org.kde.kdeconnect.BackgroundService; | ||
41 | import org.kde.kdeconnect_tp.R; | 41 | import org.kde.kdeconnect_tp.R; | ||
42 | 42 | | |||
43 | import java.util.ArrayList; | 43 | import java.util.ArrayList; | ||
44 | 44 | | |||
45 | public class CustomDevicesActivity extends AppCompatActivity { | 45 | public class CustomDevicesActivity extends AppCompatActivity { | ||
46 | 46 | | |||
47 | private static final String LOG_ID = "CustomDevicesActivity"; | 47 | private static final String LOG_ID = "CustomDevicesActivity"; | ||
48 | public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference"; | 48 | public static final String KEY_CUSTOM_DEVLIST_PREFERENCE = "device_list_preference"; | ||
49 | private static final String IP_DELIM = ","; | 49 | private static final String IP_DELIM = ","; | ||
50 | 50 | | |||
51 | private ListView list; | 51 | private ListView list; | ||
52 | 52 | | |||
53 | private ArrayList<String> ipAddressList = new ArrayList<>(); | 53 | private ArrayList<String> ipAddressList = new ArrayList<>(); | ||
54 | 54 | | |||
55 | @Override | 55 | @Override | ||
56 | protected void onCreate(Bundle savedInstanceState) { | 56 | protected void onCreate(Bundle savedInstanceState) { | ||
57 | super.onCreate(savedInstanceState); | 57 | super.onCreate(savedInstanceState); | ||
58 | initializeDeviceList(this); | 58 | initializeDeviceList(this); | ||
59 | setContentView(R.layout.custom_ip_list); | 59 | setContentView(R.layout.custom_ip_list); | ||
60 | 60 | | |||
61 | list = (ListView)findViewById(android.R.id.list); | 61 | list = (ListView) findViewById(android.R.id.list); | ||
62 | list.setOnItemClickListener(onClickListener); | 62 | list.setOnItemClickListener(onClickListener); | ||
63 | 63 | | |||
64 | list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList)); | 64 | list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList)); | ||
65 | 65 | | |||
66 | findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() { | 66 | findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() { | ||
67 | @Override | 67 | @Override | ||
68 | public void onClick(View v) { | 68 | public void onClick(View v) { | ||
69 | addNewDevice(); | 69 | addNewDevice(); | ||
70 | } | 70 | } | ||
71 | }); | 71 | }); | ||
72 | 72 | | |||
73 | EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext); | 73 | EditText ipEntryBox = (EditText) findViewById(R.id.ip_edittext); | ||
74 | ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() { | 74 | ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() { | ||
75 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { | 75 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { | ||
76 | if (actionId == EditorInfo.IME_ACTION_SEND) { | 76 | if (actionId == EditorInfo.IME_ACTION_SEND) { | ||
77 | addNewDevice(); | 77 | addNewDevice(); | ||
78 | return true; | 78 | return true; | ||
79 | } | 79 | } | ||
80 | return false; | 80 | return false; | ||
81 | } | 81 | } | ||
Show All 39 Lines | 114 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { //DismissListener | |||
121 | }); | 121 | }); | ||
122 | } | 122 | } | ||
123 | 123 | | |||
124 | builder.show(); | 124 | builder.show(); | ||
125 | } | 125 | } | ||
126 | }; | 126 | }; | ||
127 | 127 | | |||
128 | private void addNewDevice() { | 128 | private void addNewDevice() { | ||
129 | EditText ipEntryBox = (EditText)findViewById(R.id.ip_edittext); | 129 | EditText ipEntryBox = (EditText) findViewById(R.id.ip_edittext); | ||
130 | String enteredText = ipEntryBox.getText().toString().trim(); | 130 | String enteredText = ipEntryBox.getText().toString().trim(); | ||
131 | if (!enteredText.isEmpty()) { | 131 | if (!enteredText.isEmpty()) { | ||
132 | // don't add empty string (after trimming) | 132 | // don't add empty string (after trimming) | ||
133 | ipAddressList.add(enteredText); | 133 | ipAddressList.add(enteredText); | ||
134 | } | 134 | } | ||
135 | 135 | | |||
136 | saveList(); | 136 | saveList(); | ||
137 | // clear entry box | 137 | // clear entry box | ||
Show All 11 Lines | |||||
149 | private void saveList() { | 149 | private void saveList() { | ||
150 | // add entry to list and save to preferences (unless empty) | 150 | // add entry to list and save to preferences (unless empty) | ||
151 | String serialized = ""; | 151 | String serialized = ""; | ||
152 | if (!ipAddressList.isEmpty()) { | 152 | if (!ipAddressList.isEmpty()) { | ||
153 | serialized = serializeIpList(ipAddressList); | 153 | serialized = serializeIpList(ipAddressList); | ||
154 | } | 154 | } | ||
155 | PreferenceManager.getDefaultSharedPreferences(CustomDevicesActivity.this).edit().putString( | 155 | PreferenceManager.getDefaultSharedPreferences(CustomDevicesActivity.this).edit().putString( | ||
156 | KEY_CUSTOM_DEVLIST_PREFERENCE, serialized).commit(); | 156 | KEY_CUSTOM_DEVLIST_PREFERENCE, serialized).commit(); | ||
157 | ((ArrayAdapter)list.getAdapter()).notifyDataSetChanged(); | 157 | ((ArrayAdapter) list.getAdapter()).notifyDataSetChanged(); | ||
158 | 158 | | |||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | public static String serializeIpList(ArrayList<String> ipList) { | 161 | public static String serializeIpList(ArrayList<String> ipList) { | ||
162 | String serialized = ""; | 162 | String serialized = ""; | ||
163 | for (String ipAddress : ipList) { | 163 | for (String ipAddress : ipList) { | ||
164 | serialized += IP_DELIM+ipAddress; | 164 | serialized += IP_DELIM + ipAddress; | ||
165 | } | 165 | } | ||
166 | // remove first delimiter | 166 | // remove first delimiter | ||
167 | serialized = serialized.substring(IP_DELIM.length()); | 167 | serialized = serialized.substring(IP_DELIM.length()); | ||
168 | return serialized; | 168 | return serialized; | ||
169 | } | 169 | } | ||
170 | 170 | | |||
171 | public static ArrayList<String> deserializeIpList(String serialized) { | 171 | public static ArrayList<String> deserializeIpList(String serialized) { | ||
172 | ArrayList<String> ipList = new ArrayList<>(); | 172 | ArrayList<String> ipList = new ArrayList<>(); | ||
173 | for (String ipAddress : serialized.split(IP_DELIM)) { | 173 | for (String ipAddress : serialized.split(IP_DELIM)) { | ||
174 | ipList.add(ipAddress); | 174 | ipList.add(ipAddress); | ||
175 | } | 175 | } | ||
176 | return ipList; | 176 | return ipList; | ||
177 | } | 177 | } | ||
178 | 178 | | |||
179 | private void initializeDeviceList(Context context){ | 179 | private void initializeDeviceList(Context context) { | ||
180 | String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString( | 180 | String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString( | ||
181 | KEY_CUSTOM_DEVLIST_PREFERENCE, ""); | 181 | KEY_CUSTOM_DEVLIST_PREFERENCE, ""); | ||
182 | if(deviceListPrefs.isEmpty()){ | 182 | if (deviceListPrefs.isEmpty()) { | ||
183 | PreferenceManager.getDefaultSharedPreferences(context).edit().putString( | 183 | PreferenceManager.getDefaultSharedPreferences(context).edit().putString( | ||
184 | KEY_CUSTOM_DEVLIST_PREFERENCE, | 184 | KEY_CUSTOM_DEVLIST_PREFERENCE, | ||
185 | deviceListPrefs).commit(); | 185 | deviceListPrefs).commit(); | ||
186 | } else { | 186 | } else { | ||
187 | ipAddressList = deserializeIpList(deviceListPrefs); | 187 | ipAddressList = deserializeIpList(deviceListPrefs); | ||
188 | } | 188 | } | ||
189 | } | 189 | } | ||
190 | 190 | | |||
Show All 13 Lines |