Skip to content

Commit

Permalink
Feat(Core): handle EMM maanged configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Aug 29, 2024
1 parent ca47d6f commit 806add9
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
applicationId "org.glpi.inventory.agent"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 33
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -53,11 +53,16 @@ dependencies {
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.2'

/* app restriction */
implementation 'androidx.enterprise:enterprise-feedback:1.0.0'

androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.androidx', module: 'support-annotations'
})
androidTestImplementation 'tools.fastlane:screengrab:2.1.1'

androidTestImplementation 'tools.fastlane:screengrab:2.1.1'

testImplementation 'org.mockito:mockito-core:2.18.3'
androidTestImplementation 'org.mockito:mockito-android:2.18.3'

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ android:versionName="">

<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="${crashlyticsEnabled}"/>

<meta-data android:name="android.content.APP_RESTRICTIONS" android:resource="@xml/app_restrictions"/>

<activity android:exported="true" android:name="org.glpi.inventory.agent.ui.ActivitySplash" android:theme="@style/NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2712,4 +2712,4 @@
<DRIVER>wp</DRIVER>
</CONTROLLERS>
</CONTENT>
</REQUEST>
</REQUEST>
122 changes: 122 additions & 0 deletions app/src/main/java/org/glpi/inventory/agent/ui/ActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.RestrictionsManager;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.preference.PreferenceManager;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.enterprise.feedback.KeyedAppState;
import androidx.enterprise.feedback.KeyedAppStatesReporter;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
Expand All @@ -66,18 +70,27 @@

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.flyve.inventory.InventoryTask;
import org.glpi.inventory.agent.R;
import org.glpi.inventory.agent.core.detailserver.DetailServer;
import org.glpi.inventory.agent.core.detailserver.DetailServerPresenter;
import org.glpi.inventory.agent.core.main.Main;
import org.glpi.inventory.agent.core.main.MainPresenter;
import org.glpi.inventory.agent.preference.GlobalParametersPreference;
import org.glpi.inventory.agent.preference.InventoryParametersPreference;
import org.glpi.inventory.agent.schema.ServerSchema;
import org.glpi.inventory.agent.service.InventoryService;
import org.glpi.inventory.agent.utils.AgentLog;
import org.glpi.inventory.agent.utils.Helpers;
import org.glpi.inventory.agent.utils.HttpInventory;
import org.glpi.inventory.agent.utils.LocalPreferences;
import org.glpi.inventory.agent.utils.LocalStorage;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ActivityMain extends AppCompatActivity
Expand Down Expand Up @@ -115,11 +128,116 @@ public void onReceive(Context context, Intent intent) {
}
};

private BroadcastReceiver appRestrictionChange = null;
private KeyedAppStatesReporter appRestrictionChangeReporter = null;

@Override
protected void onStart() {
super.onStart();
IntentFilter restrictionsFilter =
new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);

BroadcastReceiver appRestrictionChange = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
resolveRestrictions();
}
};

registerReceiver(appRestrictionChange, restrictionsFilter);
}

@Override
protected void onStop() {
super.onStop();
if (appRestrictionChange != null) {
unregisterReceiver(appRestrictionChange);
appRestrictionChange = null;
}
}

public static void enterpriseFeedback(Context context,
String key,
String message,
String data,
int severity) {
KeyedAppStatesReporter keyedAppStatesReporter = KeyedAppStatesReporter.create(context);
KeyedAppState keyedAppStateMessage = KeyedAppState.builder()
.setSeverity(severity)
.setKey(key)
.setMessage(message)
.setData(data)
.build();
List<KeyedAppState> list = new ArrayList<>();
list.add(keyedAppStateMessage);
keyedAppStatesReporter.setStates(list);
}

private void resolveRestrictions() {
AgentLog.e("EMM - START resolve restrictions");
RestrictionsManager myRestrictionsMgr = null;
myRestrictionsMgr = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
Bundle appRestrictions = myRestrictionsMgr.getApplicationRestrictions();
Parcelable[] parcelables = appRestrictions.getParcelableArray("server_configuration_list");

if (parcelables != null && parcelables.length > 0) {
final Context context = getApplicationContext();
for (int i = 0; i < parcelables.length; i++) {
Bundle serverConfig = (Bundle) parcelables[i];
JSONObject jsonServerConfig = new JSONObject();
LocalPreferences preferences = new LocalPreferences(context);

if (serverConfig.getString("server_url").isEmpty()) {
enterpriseFeedback(getApplicationContext(), "server_url", "Error server URL is mandatory -> ", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_ERROR);
AgentLog.e("EMM - server url is mandatory");
continue;
}

try {
jsonServerConfig.put("address", serverConfig.getString("server_url"));
jsonServerConfig.put("tag", serverConfig.getString("server_tag"));
jsonServerConfig.put("login", serverConfig.getString("server_login"));
jsonServerConfig.put("pass", serverConfig.getString("server_password"));
jsonServerConfig.put("itemtype", serverConfig.getString("server_itemtype"));
jsonServerConfig.put("serial", serverConfig.getString("server_custom_asset_serial"));

AgentLog.e("EMM - Receive the following configuration '" + jsonServerConfig.toString());

JSONObject local_server = preferences.loadJSONObject(serverConfig.getString("server_url"));
AgentLog.e("EMM - Try to load '" + serverConfig.getString("server_url") + "' server if exist");
AgentLog.e("EMM - Found '" + local_server.toString() + "'");
AgentLog.e("EMM - Exist ? -> '" + !local_server.toString().equals("{}") + "'");

if (local_server.toString().equals("{}")) {
ArrayList<String> serverArray = preferences.loadServer();
serverArray.add(serverConfig.getString("server_url"));
preferences.saveServer(serverArray);
preferences.saveJSONObject(serverConfig.getString("server_url"), jsonServerConfig);
enterpriseFeedback(getApplicationContext(), "server_url", "server added successfully", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_INFO);
AgentLog.e("EMM - Server added successfully");
} else {
preferences.deletePreferences(serverConfig.getString("server_url"));
preferences.saveJSONObject(serverConfig.getString("server_url"), jsonServerConfig);
enterpriseFeedback(getApplicationContext(), "server_url", "server updated successfully", serverConfig.getString("server_url"), KeyedAppState.SEVERITY_INFO);
AgentLog.e("EMM - Server updated successfully");
}

} catch (JSONException e) {
enterpriseFeedback(getApplicationContext(), "server_url", "error while adding/updating server -> " + e.getMessage(), serverConfig.getString("server_url"), KeyedAppState.SEVERITY_ERROR);
AgentLog.e("EMM - error while adding/updating server");
AgentLog.e("EMM - " + e.getMessage());
}
}
} else {
AgentLog.e("EMM - 'server_configuration_list' key is empty");
}
AgentLog.e("EMM - END resolve restrictions");
}

@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver,new IntentFilter(InventoryService.TIMER_RECEIVER));
resolveRestrictions();
}

@Override
Expand Down Expand Up @@ -235,6 +353,10 @@ public void onClick(View view) {
}
});

//app restriction change
KeyedAppStatesReporter appRestrictionChangeReporter = KeyedAppStatesReporter.create(getApplicationContext());


}

private void disableFab(){
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/values/restrictions_values.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Choice restriction -->
<string name="title_server_itemtype">Type</string>
<string name="description_server_itemtype">Type of asset</string>
<string name="entry_server_itemtype_computer">Computer</string>
<string name="entry_server_itemtype_phone">Phone</string>
<string-array name="entries_server_itemtype">
<item>@string/entry_server_itemtype_computer</item>
<item>@string/entry_server_itemtype_phone</item>
</string-array>
<string-array name="entry_values_server_itemtype">
<item>Computer</item>
<item>Phone</item>
</string-array>
<string name="default_server_itemtype">Computer</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,14 @@
<string name="app_is_running_extend">To perform schedule inventory app is running in background</string>
<string name="disable_notification">Disable notification</string>

<!-- message on app restrictions -->
<string name="server_list">Server list</string>
<string name="server_configuration">Server configuration</string>
<string name="server_url">Server URL</string>
<string name="server_tag">Server TAG</string>
<string name="server_login">Server login</string>
<string name="server_password">Server password</string>
<string name="server_custom_asset_serial">Custom asset serial</string>
<string name="server_itemtype">Asset itemtype</string>

</resources>
46 changes: 46 additions & 0 deletions app/src/main/res/xml/app_restrictions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<restrictions xmlns:android="http://schemas.android.com/apk/res/android" >
<restriction
android:key="server_configuration_list"
android:restrictionType="bundle_array"
android:title="@string/server_list">

<restriction
android:key="server_configuration"
android:restrictionType="bundle"
android:title="@string/server_configuration">

<restriction
android:key="server_url"
android:restrictionType="string"
android:title="@string/server_url"/>
<restriction
android:key="server_tag"
android:restrictionType="string"
android:title="@string/server_tag"/>
<restriction
android:key="server_login"
android:restrictionType="string"
android:title="@string/server_login"/>
<restriction
android:key="server_password"
android:restrictionType="string"
android:title="@string/server_password"/>
<restriction
android:defaultValue="@string/default_server_itemtype"
android:description="@string/server_itemtype"
android:entries="@array/entries_server_itemtype"
android:entryValues="@array/entry_values_server_itemtype"
android:key="server_itemtype"
android:restrictionType="choice"
android:title="@string/title_server_itemtype"/>
<restriction
android:key="server_custom_asset_serial"
android:restrictionType="string"
android:title="@string/server_custom_asset_serial"/>
</restriction>



</restriction>
</restrictions>

0 comments on commit 806add9

Please sign in to comment.