|
40 | 40 | import android.content.Context;
|
41 | 41 | import android.content.Intent;
|
42 | 42 | import android.content.IntentFilter;
|
| 43 | +import android.content.RestrictionsManager; |
43 | 44 | import android.content.SharedPreferences;
|
44 | 45 | import android.content.pm.PackageManager;
|
45 | 46 | import android.os.Build;
|
46 | 47 | import android.os.Bundle;
|
47 | 48 | import android.os.Environment;
|
| 49 | +import android.os.Parcelable; |
48 | 50 | import android.preference.PreferenceManager;
|
49 | 51 |
|
50 | 52 | import androidx.appcompat.app.ActionBarDrawerToggle;
|
51 | 53 | import androidx.appcompat.app.AppCompatActivity;
|
52 | 54 | import androidx.appcompat.widget.Toolbar;
|
53 | 55 | import androidx.core.app.ActivityCompat;
|
54 | 56 | import androidx.drawerlayout.widget.DrawerLayout;
|
| 57 | +import androidx.enterprise.feedback.KeyedAppState; |
| 58 | +import androidx.enterprise.feedback.KeyedAppStatesReporter; |
55 | 59 | import androidx.fragment.app.Fragment;
|
56 | 60 | import androidx.fragment.app.FragmentManager;
|
57 | 61 | import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
66 | 70 |
|
67 | 71 | import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
68 | 72 |
|
| 73 | +import org.flyve.inventory.InventoryTask; |
69 | 74 | import org.glpi.inventory.agent.R;
|
| 75 | +import org.glpi.inventory.agent.core.detailserver.DetailServer; |
| 76 | +import org.glpi.inventory.agent.core.detailserver.DetailServerPresenter; |
70 | 77 | import org.glpi.inventory.agent.core.main.Main;
|
71 | 78 | import org.glpi.inventory.agent.core.main.MainPresenter;
|
72 | 79 | import org.glpi.inventory.agent.preference.GlobalParametersPreference;
|
73 | 80 | import org.glpi.inventory.agent.preference.InventoryParametersPreference;
|
| 81 | +import org.glpi.inventory.agent.schema.ServerSchema; |
74 | 82 | import org.glpi.inventory.agent.service.InventoryService;
|
| 83 | +import org.glpi.inventory.agent.utils.AgentLog; |
75 | 84 | import org.glpi.inventory.agent.utils.Helpers;
|
| 85 | +import org.glpi.inventory.agent.utils.HttpInventory; |
76 | 86 | import org.glpi.inventory.agent.utils.LocalPreferences;
|
77 | 87 | import org.glpi.inventory.agent.utils.LocalStorage;
|
| 88 | +import org.json.JSONException; |
| 89 | +import org.json.JSONObject; |
78 | 90 |
|
79 | 91 | import java.util.ArrayList;
|
80 | 92 | import java.util.Collections;
|
| 93 | +import java.util.List; |
81 | 94 | import java.util.Map;
|
82 | 95 |
|
83 | 96 | public class ActivityMain extends AppCompatActivity
|
@@ -115,11 +128,150 @@ public void onReceive(Context context, Intent intent) {
|
115 | 128 | }
|
116 | 129 | };
|
117 | 130 |
|
| 131 | + private BroadcastReceiver appRestrictionChange = null; |
| 132 | + private KeyedAppStatesReporter appRestrictionChangeReporter = null; |
| 133 | + |
| 134 | + @Override |
| 135 | + protected void onStart() { |
| 136 | + super.onStart(); |
| 137 | + IntentFilter restrictionsFilter = |
| 138 | + new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 139 | + |
| 140 | + BroadcastReceiver appRestrictionChange = new BroadcastReceiver() { |
| 141 | + @Override public void onReceive(Context context, Intent intent) { |
| 142 | + resolveRestrictions(); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + registerReceiver(appRestrictionChange, restrictionsFilter); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void onStop() { |
| 151 | + super.onStop(); |
| 152 | + if (appRestrictionChange != null) { |
| 153 | + unregisterReceiver(appRestrictionChange); |
| 154 | + appRestrictionChange = null; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public static void enterpriseFeedback(Context context, |
| 159 | + String key, |
| 160 | + String message, |
| 161 | + String data, |
| 162 | + int severity) { |
| 163 | + KeyedAppStatesReporter keyedAppStatesReporter = KeyedAppStatesReporter.create(context); |
| 164 | + KeyedAppState keyedAppStateMessage = KeyedAppState.builder() |
| 165 | + .setSeverity(severity) |
| 166 | + .setKey(key) |
| 167 | + .setMessage(message) |
| 168 | + .setData(data) |
| 169 | + .build(); |
| 170 | + List<KeyedAppState> list = new ArrayList<>(); |
| 171 | + list.add(keyedAppStateMessage); |
| 172 | + keyedAppStatesReporter.setStates(list); |
| 173 | + } |
| 174 | + |
| 175 | + private void resolveRestrictions() { |
| 176 | + AgentLog.e("EMM - START resolve restrictions"); |
| 177 | + RestrictionsManager myRestrictionsMgr = null; |
| 178 | + myRestrictionsMgr = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE); |
| 179 | + Bundle appRestrictions = myRestrictionsMgr.getApplicationRestrictions(); |
| 180 | + |
| 181 | + SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
| 182 | + SharedPreferences.Editor editor = customSharedPreference.edit(); |
| 183 | + |
| 184 | + if (appRestrictions.containsKey("automatic_inventory")) { |
| 185 | + editor.putBoolean("autoStartInventory", appRestrictions.getBoolean("automatic_inventory")); |
| 186 | + enterpriseFeedback(getApplicationContext(), "automatic_inventory", "automatic_inventory option set successfully", appRestrictions.getBoolean("automatic_inventory") ? "true" : "false", KeyedAppState.SEVERITY_INFO); |
| 187 | + AgentLog.e("EMM - set automatic inventory to " + appRestrictions.getBoolean("automatic_inventory")); |
| 188 | + editor.apply(); |
| 189 | + } |
| 190 | + |
| 191 | + if (appRestrictions.containsKey("frequency")) { |
| 192 | + editor.putString("timeInventory", appRestrictions.getString("frequency")); |
| 193 | + enterpriseFeedback(getApplicationContext(), "frequency", "frequency option set successfully", appRestrictions.getString("frequency"), KeyedAppState.SEVERITY_INFO); |
| 194 | + AgentLog.e("EMM - set frequency to " + appRestrictions.getString("frequency")); |
| 195 | + editor.apply(); |
| 196 | + } |
| 197 | + |
| 198 | + if (appRestrictions.containsKey("auto_start_on_boot")) { |
| 199 | + editor.putBoolean("boot", appRestrictions.getBoolean("auto_start_on_boot")); |
| 200 | + enterpriseFeedback(getApplicationContext(), "auto_start_on_boot", "auto_start_on_boot option set successfully", appRestrictions.getBoolean("auto_start_on_boot") ? "true" : "false", KeyedAppState.SEVERITY_INFO); |
| 201 | + AgentLog.e("EMM - set auto start on boot to " + appRestrictions.getBoolean("auto_start_on_boot")); |
| 202 | + editor.apply(); |
| 203 | + } |
| 204 | + |
| 205 | + Parcelable[] parcelables = appRestrictions.getParcelableArray("server_configuration_list"); |
| 206 | + if (parcelables != null && parcelables.length > 0) { |
| 207 | + final Context context = getApplicationContext(); |
| 208 | + for (int i = 0; i < parcelables.length; i++) { |
| 209 | + Bundle serverConfig = (Bundle) parcelables[i]; |
| 210 | + JSONObject jsonServerConfig = new JSONObject(); |
| 211 | + LocalPreferences preferences = new LocalPreferences(context); |
| 212 | + |
| 213 | + if (serverConfig.getString("server_url").isEmpty()) { |
| 214 | + enterpriseFeedback(getApplicationContext(), "server_url", "Error server URL is mandatory -> ", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_ERROR); |
| 215 | + AgentLog.e("EMM - server url is mandatory"); |
| 216 | + continue; |
| 217 | + } |
| 218 | + |
| 219 | + try { |
| 220 | + jsonServerConfig.put("address", serverConfig.getString("server_url")); |
| 221 | + jsonServerConfig.put("tag", serverConfig.getString("server_tag")); |
| 222 | + jsonServerConfig.put("login", serverConfig.getString("server_login")); |
| 223 | + jsonServerConfig.put("pass", serverConfig.getString("server_password")); |
| 224 | + jsonServerConfig.put("itemtype", serverConfig.getString("server_itemtype")); |
| 225 | + jsonServerConfig.put("serial", serverConfig.getString("server_custom_asset_serial")); |
| 226 | + |
| 227 | + AgentLog.e("EMM - Receive the following configuration '" + jsonServerConfig.toString()); |
| 228 | + |
| 229 | + JSONObject local_server = preferences.loadJSONObject(serverConfig.getString("server_url")); |
| 230 | + AgentLog.e("EMM - Try to load '" + serverConfig.getString("server_url") + "' server if exist"); |
| 231 | + AgentLog.e("EMM - Found '" + local_server.toString() + "'"); |
| 232 | + AgentLog.e("EMM - Exist ? -> '" + !local_server.toString().equals("{}") + "'"); |
| 233 | + |
| 234 | + if (local_server.toString().equals("{}")) { |
| 235 | + ArrayList<String> serverArray = preferences.loadServer(); |
| 236 | + serverArray.add(serverConfig.getString("server_url")); |
| 237 | + preferences.saveServer(serverArray); |
| 238 | + preferences.saveJSONObject(serverConfig.getString("server_url"), jsonServerConfig); |
| 239 | + enterpriseFeedback(getApplicationContext(), "server_url", "server_url added successfully", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_INFO); |
| 240 | + enterpriseFeedback(getApplicationContext(), "server_tag", "server_tag added successfully", serverConfig.getString("server_tag"), KeyedAppState.SEVERITY_INFO); |
| 241 | + enterpriseFeedback(getApplicationContext(), "server_login", "server_login added successfully", serverConfig.getString("server_login"), KeyedAppState.SEVERITY_INFO); |
| 242 | + enterpriseFeedback(getApplicationContext(), "server_password", "server_password added successfully", "***", KeyedAppState.SEVERITY_INFO); |
| 243 | + enterpriseFeedback(getApplicationContext(), "server_itemtype", "server_itemtype added successfully", serverConfig.getString("server_itemtype"), KeyedAppState.SEVERITY_INFO); |
| 244 | + enterpriseFeedback(getApplicationContext(), "server_custom_asset_serial", "server_custom_asset_serial added successfully", serverConfig.getString("server_custom_asset_serial"), KeyedAppState.SEVERITY_INFO); |
| 245 | + AgentLog.e("EMM - Server added successfully"); |
| 246 | + } else { |
| 247 | + preferences.deletePreferences(serverConfig.getString("server_url")); |
| 248 | + preferences.saveJSONObject(serverConfig.getString("server_url"), jsonServerConfig); |
| 249 | + enterpriseFeedback(getApplicationContext(), "server_url", "server_url updated successfully", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_INFO); |
| 250 | + enterpriseFeedback(getApplicationContext(), "server_tag", "server_tag updated successfully", serverConfig.getString("server_tag"), KeyedAppState.SEVERITY_INFO); |
| 251 | + enterpriseFeedback(getApplicationContext(), "server_login", "server_login updated successfully", serverConfig.getString("server_login"), KeyedAppState.SEVERITY_INFO); |
| 252 | + enterpriseFeedback(getApplicationContext(), "server_password", "server_password updated successfully", "***", KeyedAppState.SEVERITY_INFO); |
| 253 | + enterpriseFeedback(getApplicationContext(), "server_itemtype", "server_itemtype updated successfully", serverConfig.getString("server_itemtype"), KeyedAppState.SEVERITY_INFO); |
| 254 | + enterpriseFeedback(getApplicationContext(), "server_custom_asset_serial", "server_custom_asset_serial updated successfully", serverConfig.getString("server_custom_asset_serial"), KeyedAppState.SEVERITY_INFO); |
| 255 | + AgentLog.e("EMM - Server updated successfully"); |
| 256 | + } |
| 257 | + |
| 258 | + } catch (JSONException e) { |
| 259 | + enterpriseFeedback(getApplicationContext(), "server_url", "error while adding/updating server -> " + e.getMessage(), serverConfig.getString("server_url"), KeyedAppState.SEVERITY_ERROR); |
| 260 | + AgentLog.e("EMM - error while adding/updating server"); |
| 261 | + AgentLog.e("EMM - " + e.getMessage()); |
| 262 | + } |
| 263 | + } |
| 264 | + } else { |
| 265 | + AgentLog.e("EMM - 'server_configuration_list' key is empty"); |
| 266 | + } |
| 267 | + AgentLog.e("EMM - END resolve restrictions"); |
| 268 | + } |
118 | 269 |
|
119 | 270 | @Override
|
120 | 271 | protected void onResume() {
|
121 | 272 | super.onResume();
|
122 | 273 | registerReceiver(broadcastReceiver,new IntentFilter(InventoryService.TIMER_RECEIVER));
|
| 274 | + resolveRestrictions(); |
123 | 275 | }
|
124 | 276 |
|
125 | 277 | @Override
|
@@ -235,6 +387,10 @@ public void onClick(View view) {
|
235 | 387 | }
|
236 | 388 | });
|
237 | 389 |
|
| 390 | + //app restriction change |
| 391 | + KeyedAppStatesReporter appRestrictionChangeReporter = KeyedAppStatesReporter.create(getApplicationContext()); |
| 392 | + |
| 393 | + |
238 | 394 | }
|
239 | 395 |
|
240 | 396 | private void disableFab(){
|
|
0 commit comments