|
| 1 | +package vendor.datalogic.service.eventprofiles; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.net.ConnectivityManager; |
| 7 | +import android.net.NetworkRequest; |
| 8 | +import android.net.NetworkRequest.Builder; |
| 9 | +import android.net.Network; |
| 10 | +import android.net.NetworkCapabilities; |
| 11 | +import android.os.Bundle; |
| 12 | +import androidx.appcompat.app.AppCompatActivity; |
| 13 | + |
| 14 | +import android.util.Log; |
| 15 | +import android.widget.Toast; |
| 16 | + |
| 17 | +import com.datalogic.device.PersistenceType; |
| 18 | +import com.datalogic.device.configuration.ConfigException; |
| 19 | +import com.datalogic.device.configuration.ProfileManager; |
| 20 | +import com.datalogic.device.configuration.PropertyID; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | + |
| 24 | +public class MainActivity extends AppCompatActivity { |
| 25 | + |
| 26 | + private String TAG="EventProfiles"; |
| 27 | + private ConnectivityManager connectivityManager; |
| 28 | + private ConnectivityManager.NetworkCallback networkCallback; |
| 29 | + private ProfileManager pm; |
| 30 | + private HashMap map; |
| 31 | + @Override |
| 32 | + protected void onCreate(Bundle savedInstanceState) { |
| 33 | + super.onCreate(savedInstanceState); |
| 34 | + Log.d(TAG,"onCreate is called"); |
| 35 | + setContentView(R.layout.activity_main); |
| 36 | + |
| 37 | + // 1. Setting the wifi connectivity |
| 38 | + // to notify when Access Point has been changed |
| 39 | + setUp(); |
| 40 | + |
| 41 | + // 2. Create new instance of ProfileManager with context |
| 42 | + pm = new ProfileManager(this); |
| 43 | + |
| 44 | + // 3. Create an HashMap and add it the couples {PropertyID,value} |
| 45 | + // to be set applying the profile. |
| 46 | + map = new HashMap(); |
| 47 | + map.put(PropertyID.GREEN_SPOT_ENABLE, "false"); // BooleanProperty |
| 48 | + map.put(PropertyID.DEVICE_NAME_BASE , "wifi"); // TextProperty |
| 49 | + |
| 50 | + // 4. Create the profile calling createProfile() |
| 51 | + pm.createProfile( "wifi_test.json", |
| 52 | + map, |
| 53 | + "Wifi Test Profile", |
| 54 | + PersistenceType.ENTERPRISE_RESET_PERSISTENT //persistent to device reboots |
| 55 | + ); |
| 56 | + |
| 57 | + // When wifi connection is ready, profile wifi_test.json will be loaded (line 75) |
| 58 | + // When wifi connection is lost, profile wifi_test.json will be unloaded (line 83). |
| 59 | + } |
| 60 | + |
| 61 | + private void setUp() { |
| 62 | + // 1. Setting the wifi connectivity |
| 63 | + connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
| 64 | + // Check for permission (ACCESS_FINE_LOCATION required on devices running API 23+) |
| 65 | + if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
| 66 | + registerNetworkCallback(); |
| 67 | + } else { |
| 68 | + requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void registerNetworkCallback() { |
| 73 | + networkCallback = new ConnectivityManager.NetworkCallback() { |
| 74 | + @Override |
| 75 | + public void onAvailable(Network network) { |
| 76 | + // Network available, check if it's Wi-Fi |
| 77 | + ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
| 78 | + NetworkCapabilities capabilities = cm.getNetworkCapabilities(network); |
| 79 | + if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 80 | + Toast.makeText(MainActivity.this, "Connected to Wi-Fi", Toast.LENGTH_SHORT).show(); |
| 81 | + pm.loadProfile("wifi_test.json"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onLost(Network network) { |
| 87 | + // Wi-Fi or mobile data network lost |
| 88 | + Toast.makeText(MainActivity.this, "Network lost", Toast.LENGTH_SHORT).show(); |
| 89 | + pm.unloadProfile(); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder(); |
| 94 | + requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); // Listen only for Wi-Fi |
| 95 | + connectivityManager.registerNetworkCallback(requestBuilder.build(), networkCallback); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void onPostResume() { |
| 100 | + super.onPostResume(); |
| 101 | + pm.createProfile( "wifi_test.json", |
| 102 | + map, |
| 103 | + "Wifi Test Profile", |
| 104 | + PersistenceType.ENTERPRISE_RESET_PERSISTENT //persistent to device reboots |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void onStop() { |
| 110 | + super.onStop(); |
| 111 | + Log.d(TAG, "onDestroy is called"); |
| 112 | + if (networkCallback != null) { |
| 113 | + connectivityManager.unregisterNetworkCallback(networkCallback); |
| 114 | + } |
| 115 | + pm.deleteProfile("wifi_test.json"); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments