From d40119e760963c3e624579c3569a2e74a5273ffc Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Fri, 21 Sep 2018 20:19:38 +0200 Subject: [PATCH] WIP - https://github.com/Catfriend1/syncthing-android/issues/57 --- .../activities/FolderActivity.java | 112 +++++++++++++++++- .../views/WifiSsidPreference.java | 5 +- .../res/drawable-hdpi/ic_wifi_black_24dp.png | Bin 0 -> 387 bytes .../res/drawable-mdpi/ic_wifi_black_24dp.png | Bin 0 -> 269 bytes .../res/drawable-xhdpi/ic_wifi_black_24dp.png | Bin 0 -> 483 bytes .../drawable-xxhdpi/ic_wifi_black_24dp.png | Bin 0 -> 695 bytes .../drawable-xxxhdpi/ic_wifi_black_24dp.png | Bin 0 -> 883 bytes app/src/main/res/layout/fragment_folder.xml | 32 +++-- .../main/res/layout/item_wifi_ssid_form.xml | 11 ++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 5 + 11 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_wifi_black_24dp.png create mode 100644 app/src/main/res/drawable-mdpi/ic_wifi_black_24dp.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_wifi_black_24dp.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_wifi_black_24dp.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_wifi_black_24dp.png create mode 100644 app/src/main/res/layout/item_wifi_ssid_form.xml diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java index 5d230b6e6..2475b7c21 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java @@ -5,8 +5,11 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; +import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.v4.provider.DocumentFile; @@ -41,9 +44,13 @@ import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; import static android.support.v4.view.MarginLayoutParamsCompat.setMarginEnd; @@ -97,6 +104,7 @@ public class FolderActivity extends SyncthingActivity private TextView mFolderTypeDescriptionView; private SwitchCompat mFolderFileWatcher; private SwitchCompat mFolderPaused; + private ViewGroup mWifiSsidContainer; private ViewGroup mDevicesContainer; private TextView mPullOrderTypeView; private TextView mPullOrderDescriptionView; @@ -171,6 +179,7 @@ public void onCreate(Bundle savedInstanceState) { mPullOrderDescriptionView = findViewById(R.id.pullOrderDescription); mVersioningDescriptionView = findViewById(R.id.versioningDescription); mVersioningTypeView = findViewById(R.id.versioningType); + mWifiSsidContainer = findViewById(R.id.wifiSsidContainer); mDevicesContainer = findViewById(R.id.devicesContainer); mEditIgnoreListTitle = findViewById(R.id.edit_ignore_list_title); mEditIgnoreListContent = findViewById(R.id.edit_ignore_list_content); @@ -416,8 +425,25 @@ private void updateViewsAndSetListeners() { updateVersioningDescription(); mFolderFileWatcher.setChecked(mFolder.fsWatcherEnabled); mFolderPaused.setChecked(mFolder.paused); - List devicesList = getApi().getDevices(false); + // Populate wifiSsidAvailList. + List wifiSsidAvailList = new ArrayList(); + WifiConfiguration[] wifiNetworks = loadConfiguredNetworksSorted(); + if (wifiNetworks != null) { + // Display without surrounding quotes. + wifiSsidAvailList = extractSsid(wifiNetworks, true); + } + mWifiSsidContainer.removeAllViews(); + if (wifiSsidAvailList.isEmpty()) { + addEmptyWifiSsidListView(); + } else { + for (String wifiSsid : wifiSsidAvailList) { + addWifiSsidViewAndSetListener(wifiSsid, getLayoutInflater()); + } + } + + // Populate devicesList. + List devicesList = getApi().getDevices(false); mDevicesContainer.removeAllViews(); if (devicesList.isEmpty()) { addEmptyDeviceListView(); @@ -628,6 +654,29 @@ private void initFolder() { mFolder.versioning = new Folder.Versioning(); } + private void addEmptyWifiSsidListView() { + int height = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, height); + int dividerInset = getResources().getDimensionPixelOffset(R.dimen.material_divider_inset); + int contentInset = getResources().getDimensionPixelOffset(R.dimen.abc_action_bar_content_inset_material); + setMarginStart(params, dividerInset); + setMarginEnd(params, contentInset); + TextView emptyView = new TextView(mWifiSsidContainer.getContext()); + emptyView.setGravity(CENTER_VERTICAL); + emptyView.setText(R.string.devices_list_empty); + mWifiSsidContainer.addView(emptyView, params); + } + + private void addWifiSsidViewAndSetListener(String wifiSsid, LayoutInflater inflater) { + inflater.inflate(R.layout.item_wifi_ssid_form, mWifiSsidContainer); + SwitchCompat wifiSsidView = (SwitchCompat) mWifiSsidContainer.getChildAt(mWifiSsidContainer.getChildCount()-1); + wifiSsidView.setOnCheckedChangeListener(null); + // ToDo wifiSsidView.setChecked(mFolder.getDevice(device.deviceID) != null); + wifiSsidView.setText(wifiSsid); + wifiSsidView.setTag(wifiSsid); + wifiSsidView.setOnCheckedChangeListener(mCheckedListener); + } + private void addEmptyDeviceListView() { int height = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, height); @@ -825,4 +874,65 @@ private void setVersioningDescription(String type, String description) { mVersioningTypeView.setText(type); mVersioningDescriptionView.setText(description); } + + /** + * Removes any network that is no longer saved on the device. Otherwise it will never be + * removed from the allowed set by MultiSelectListPreference. + */ + private void filterRemovedNetworks(Set selected, CharSequence[] all) { + HashSet availableNetworks = new HashSet<>(Arrays.asList(all)); + selected.retainAll(availableNetworks); + } + + /** + * Converts WiFi configuration to a list representation, using the SSID. + * + * It can also remove surrounding quotes which indicate that the SSID is an UTF-8 + * string and not a Hex-String, if the strings are intended to be displayed to the + * user, who will not expect the quotes. + * + * @param configs the objects to convert + * @param stripQuotes if to remove surrounding quotes + * @return the formatted SSID of the wifi configurations + */ + private List extractSsid(WifiConfiguration[] configs, boolean stripQuotes) { + List result = new ArrayList(); + for (int i = 0; i < configs.length; i++) { + // See #620: there may be null-SSIDs + String ssid = configs[i].SSID != null ? configs[i].SSID : ""; + // WiFi SSIDs can either be UTF-8 (encapsulated in '"') or hex-strings + if (stripQuotes) { + result.add(ssid.replaceFirst("^\"", "").replaceFirst("\"$", "")); + } else { + result.add(ssid); + } + } + return result; + } + + /** + * Load the configured WiFi networks, sort them by SSID. + * + * @return a sorted array of WifiConfiguration, or null, if data cannot be retrieved + */ + private WifiConfiguration[] loadConfiguredNetworksSorted() { + WifiManager wifiManager = (WifiManager) + getApplicationContext().getSystemService(Context.WIFI_SERVICE); + if (wifiManager != null) { + List configuredNetworks = wifiManager.getConfiguredNetworks(); + // if WiFi is turned off, getConfiguredNetworks returns null on many devices + if (configuredNetworks != null) { + WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]); + Arrays.sort(result, (lhs, rhs) -> { + // See #620: There may be null-SSIDs + String l = lhs.SSID != null ? lhs.SSID : ""; + String r = rhs.SSID != null ? rhs.SSID : ""; + return l.compareToIgnoreCase(r); + }); + return result; + } + } + // WiFi is turned off or device doesn't have WiFi + return null; + } } diff --git a/app/src/main/java/com/nutomic/syncthingandroid/views/WifiSsidPreference.java b/app/src/main/java/com/nutomic/syncthingandroid/views/WifiSsidPreference.java index 92e3c3894..aa2d66a4b 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/views/WifiSsidPreference.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/views/WifiSsidPreference.java @@ -115,10 +115,11 @@ private CharSequence[] extractSsid(WifiConfiguration[] configs, boolean stripQuo // See #620: there may be null-SSIDs String ssid = configs[i].SSID != null ? configs[i].SSID : ""; // WiFi SSIDs can either be UTF-8 (encapsulated in '"') or hex-strings - if (stripQuotes) + if (stripQuotes) { result[i] = ssid.replaceFirst("^\"", "").replaceFirst("\"$", ""); - else + } else { result[i] = ssid; + } } return result; } diff --git a/app/src/main/res/drawable-hdpi/ic_wifi_black_24dp.png b/app/src/main/res/drawable-hdpi/ic_wifi_black_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..b646ff25a216aeb8f13725f6f1e4fb8871fed6e6 GIT binary patch literal 387 zcmV-}0et?6P)NklcgWVsopsi`Pg8h)(`)>9 z{P&mXWtgdEYEoyWsfH=rbjuv~LP&bygyp6wPA^^V3Prl3OE38}x+#>{O^tqQjKe~i z9WX{>mIp%2d)I8xZnnt=7-+It+O2oZ2O*~0Y>`D?#)EpSFiM_LR_OU<00n^hxZ5LZ z3{uQst2~KUX#mK+@3>*SQjK@x`&FAjyp>}HiWs584tt$*%0An47=~z|V}eX#v8b1V zi{1-K?_E?#ugcftz97H*nkcT*TcJpAbtA0@f+-+1#f3Pj;tG1p(_29W`dFY%n+5u)LaT0ZdA6xgZL-oos8gX#5X+$YTV+uF htum^Y$^7?M`UEtS0iB*SewP3M002ovPDHLkV1lXdwq*bS literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-mdpi/ic_wifi_black_24dp.png b/app/src/main/res/drawable-mdpi/ic_wifi_black_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..f34ac53f431dabe805105f980cee9eea7fef3eab GIT binary patch literal 269 zcmV+o0rLKdP)k3$r-xWUgN7j_(8 z-uK!I51zHO=fd%dM?nhT?J_ T@dA5}00000NkvXXu0mjf>dbXd literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_wifi_black_24dp.png b/app/src/main/res/drawable-xhdpi/ic_wifi_black_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..61596339694b037b5a110f905decc00deaa8be0f GIT binary patch literal 483 zcmV<90UZ8`P)vo0W`C?oe%GQN+e);T;RTYn{dh3%TA)R$) z38VIfI7glt(<`OdnCHo%eWM~%PKBsl)4Da%Z94H=Pfe*aBSh_(&@Qib6Ly5C82~b& zu0yjroL7fgheBN$sflfUit4i+pN}VA>O|~GP0zg4k8_ zeh7)aTQDHjWutGZ zg8EKWjkpM)-H57qCZov2v8T#XE?aV(|9pC4(-mjwwOQ;uWR{gQQnD=be<&Gp&s{@G znyzfgf$+BjOUjz)X5!uY>6WI+ghZJpst8dPO;#yzzVO7KfsLn`6+n`Ppqq5#N5Lo< Z1psfp#t)8f7R~?w002ovPDHLkV1kp!-Wvb_ literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_wifi_black_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_wifi_black_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..06ae9d00879a471aff594aca8872f93f815eef5e GIT binary patch literal 695 zcmV;o0!aOdP)swsx^?+sQ7rZQC|(Y}>YN+fG)&*z7yoL7{PfEb6@be!ecNR4SE9rP5h+ zvxIG(>N2-_*o$8Bh+AFmRNGlnH|=I|`?%GYCW!@2_NCkHYe{XUm#sZwnAl*$J!)IM zB(;&-jTT#Ilsj!AfiAZ4xtOTWY^_TxV|MVJn7AM9(A=oieId3{pITc(7VwbR#yx5w zb=uzqvF@fC>SrJL(9ixiRjj-54pM56Cq(6a?=t(@(7d|La^1~q1N*tM&a3YUgA`cZ z-(q2-J?>z0smELn_P8-(VSig)j?GLIbDQK^8|tA^J#66G!sq%#o5`@JX<|-O++uF6 znAgn(*Vi8oEjdff?N7Jc z&2sty7-%`Wx!qr4Zf9vx$o%-c(1MCA=)%ko;Q(nTO?8{O)MIY9nwB#s*`z-#s}alk zBWF&MNpBjWQA50!ehvh;>!umq-7e@fDoZ%tjh^<5n;dT`9jKqfGQI$QbfmsIz}7~J zIgPZPc6LO`S7XgF+SOTNNoQ$iK`bcf0HJfu$)6N-ppbS>`bj}25Yo&^KPl)$f}m2V dR4SDUrUMQu1+TV8a@GI<002ovPDHLkV1gwvV3+^^ literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxxhdpi/ic_wifi_black_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_wifi_black_24dp.png new file mode 100644 index 0000000000000000000000000000000000000000..b09716aa9080d5562384369335c65a130d7fbb8d GIT binary patch literal 883 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD0wg^q?%&M7z-;B|;uuoF`1XeLoRn~hV-J_# z-I#lO+uYPMp*hw&U$0ED+1v%>=iZhw^E=}>_jc^}va5g2eZFtDZ+m2@kmn>7F#4l> zF-z=ovNifo@_L@f>TTZ5yCx^FIOAd-Uw70=x3bAQ&lDfy-1+++-=b0py(d{~tmh{9 zo=q}|zRdZ~B<#dhk6b04XO)V(`g=F*EUUek$9=d~$>sZ=qahnt=`Z$|Pl^3|Jw>*8 zQF={g=&~H|{GZiO=;_ z!KV4GT3@ZMscc&HzGUIfsfl*tFV+_My6xlCk8+-Q?L*R+pMt_}uLHLEEml6)KjXt% zSrPqzA05~}3ePi|zh{vYSCIP)uIX`kyi z-@GTVo;xsgiv2^r_6Qf#ZmT1*OXtZOF1&6UHtG1Ob(;VF+~8av5gotg_0p#E{WA>i zaYlU!?H2CQe)Fe;vp+JXUialgCt!#a>{Hu%;mMq$^X+Fiwwk!4-I}OkeEaUyd0!WP zJof9t=5-s|XPUN5jY)qb5*NDfoY$*cm)5QPCG^pBTF~-sUnUD&^xqcJ&Uane<=WeV zQgz`+lUF*6DsJL3dDlV+cd> + + + - + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0aa4055ea..d1c3fa480 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -188,6 +188,9 @@ Please report any problems you encounter via Github. Pause Folder + + WiFi networks + Devices diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index d88d22f0c..3dc6477ef 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -32,6 +32,11 @@ 8dp + +