-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7d089
commit ed64bfd
Showing
27 changed files
with
1,157 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
app/src/main/java/com/espressif/esptouch/android/EspTouchActivityAbs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package com.espressif.esptouch.android; | ||
|
||
import android.Manifest; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.location.LocationManager; | ||
import android.net.wifi.WifiInfo; | ||
import android.net.wifi.WifiManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.text.Spannable; | ||
import android.text.SpannableString; | ||
import android.text.SpannableStringBuilder; | ||
import android.text.style.ForegroundColorSpan; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.app.AlertDialog; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.location.LocationManagerCompat; | ||
|
||
import com.espressif.iot_esptouch_demo.R; | ||
import com.espressif.iot_esptouch_demo.NetUtils; | ||
|
||
import java.net.InetAddress; | ||
|
||
public abstract class EspTouchActivityAbs extends AppCompatActivity { | ||
private static final int MENU_ITEM_ABOUT = 0; | ||
|
||
private WifiManager mWifiManager; | ||
|
||
protected abstract String getEspTouchVersion(); | ||
|
||
protected static class StateResult { | ||
public CharSequence message = null; | ||
|
||
public boolean permissionGranted = false; | ||
|
||
public boolean locationRequirement = false; | ||
|
||
public boolean wifiConnected = false; | ||
public boolean is5G = false; | ||
public InetAddress address = null; | ||
public String ssid = null; | ||
public byte[] ssidBytes = null; | ||
public String bssid = null; | ||
} | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
mWifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); | ||
} | ||
|
||
private void showAboutDialog() { | ||
String esptouchVerText = getEspTouchVersion(); | ||
String appVer = ""; | ||
PackageManager packageManager = getPackageManager(); | ||
try { | ||
PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0); | ||
appVer = info.versionName; | ||
} catch (PackageManager.NameNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
CharSequence[] items = new CharSequence[]{ | ||
getString(R.string.about_app_version, appVer), | ||
esptouchVerText | ||
}; | ||
new AlertDialog.Builder(this) | ||
.setTitle(R.string.menu_item_about) | ||
.setIcon(R.drawable.baseline_info_black_24) | ||
.setItems(items, null) | ||
.show(); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
menu.add(Menu.NONE, MENU_ITEM_ABOUT, 0, R.string.menu_item_about) | ||
.setIcon(R.drawable.ic_info_outline_white_24dp) | ||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); | ||
return super.onCreateOptionsMenu(menu); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
onBackPressed(); | ||
return true; | ||
case MENU_ITEM_ABOUT: | ||
showAboutDialog(); | ||
return true; | ||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
protected StateResult checkPermission() { | ||
StateResult result = new StateResult(); | ||
result.permissionGranted = false; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
boolean locationGranted = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) | ||
== PackageManager.PERMISSION_GRANTED; | ||
if (!locationGranted) { | ||
String[] splits = getString(R.string.esptouch_message_permission).split("\n"); | ||
if (splits.length != 2) { | ||
throw new IllegalArgumentException("Invalid String @RES esptouch_message_permission"); | ||
} | ||
SpannableStringBuilder ssb = new SpannableStringBuilder(splits[0]); | ||
ssb.append('\n'); | ||
SpannableString clickMsg = new SpannableString(splits[1]); | ||
ForegroundColorSpan clickSpan = new ForegroundColorSpan(0xFF0022FF); | ||
clickMsg.setSpan(clickSpan, 0, clickMsg.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); | ||
ssb.append(clickMsg); | ||
result.message = ssb; | ||
return result; | ||
} | ||
} | ||
|
||
result.permissionGranted = true; | ||
return result; | ||
} | ||
|
||
protected StateResult checkLocation() { | ||
StateResult result = new StateResult(); | ||
result.locationRequirement = true; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
LocationManager manager = getSystemService(LocationManager.class); | ||
boolean enable = manager != null && LocationManagerCompat.isLocationEnabled(manager); | ||
if (!enable) { | ||
result.message = getString(R.string.esptouch_message_location); | ||
return result; | ||
} | ||
} | ||
|
||
result.locationRequirement = false; | ||
return result; | ||
} | ||
|
||
protected StateResult checkWifi() { | ||
StateResult result = new StateResult(); | ||
result.wifiConnected = false; | ||
WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); | ||
boolean connected = NetUtils.isWifiConnected(mWifiManager); | ||
if (!connected) { | ||
result.message = getString(R.string.esptouch_message_wifi_connection); | ||
return result; | ||
} | ||
|
||
String ssid = NetUtils.getSsidString(wifiInfo); | ||
int ipValue = wifiInfo.getIpAddress(); | ||
if (ipValue != 0) { | ||
result.address = NetUtils.getAddress(wifiInfo.getIpAddress()); | ||
} else { | ||
result.address = NetUtils.getIPv4Address(); | ||
if (result.address == null) { | ||
result.address = NetUtils.getIPv6Address(); | ||
} | ||
} | ||
|
||
result.wifiConnected = true; | ||
result.message = ""; | ||
result.is5G = NetUtils.is5G(wifiInfo.getFrequency()); | ||
if (result.is5G) { | ||
result.message = getString(R.string.esptouch_message_wifi_frequency); | ||
} | ||
result.ssid = ssid; | ||
result.ssidBytes = NetUtils.getRawSsidBytesOrElse(wifiInfo, ssid.getBytes()); | ||
result.bssid = wifiInfo.getBSSID(); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.