Commits

Stephen Smalley  committed c5dbeaf

Save settings immediately, handle SELinux disabled and no booleans cases, and remove unneeded libraries.

Author: James Carter <jwcart2@tycho.nsa.gov>

- Save enforcing and boolean values immediately when set, not on shutdown.
- Display SELinux disabled fragment when SELinux is disabled.
- Display empty list message if boolean list is empty.
- Remove unneeded libraries.

  • Participants
  • Parent commits cd01e73

Comments (0)

Files changed (9)

 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_JAVA_LIBRARIES := bouncycastle
-LOCAL_STATIC_JAVA_LIBRARIES := guava
-
 LOCAL_MODULE_TAGS := optional
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)

File AndroidManifest.xml

             </intent-filter>
         </receiver>
 
-        <receiver android:name="SaveSettings" android:exported="false">
-            <intent-filter android:priority="1">
-                <action android:name="android.intent.action.ACTION_SHUTDOWN"/>
-                <action android:name="android.intent.action.REBOOT"/>
-            </intent-filter>
-        </receiver>
-
     </application>
 
 </manifest> 

File res/values/strings.xml

     <string name="activity_name">SEAndroidManager</string>
     <string name="app_label">SEAndroid Manager</string>
     <string name="app_label_launcher">SEAndroid Manager</string>
+    <string name="selinux_disabled_fragment_title">SELinux Disabled</string>
     <string name="selinux_enforcing_fragment_title">Change SELinux Enforcing Mode</string>
     <string name="selinux_boolean_fragment_title">Manage SELinux Booleans</string>
     <string name="selinux_not_enabled">SELinux is not enabled</string>

File res/xml/disabled_headers.xml

+<?xml version="1.0" encoding="utf-8"?>
+<preference-headers
+    xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <header
+        android:fragment="com.android.seandroid_manager.SEAndroidManager$SELinuxDisabledFragment"
+        android:title="@string/selinux_disabled_fragment_title">
+    </header>
+
+</preference-headers>

File res/xml/enabled_headers.xml

+<?xml version="1.0" encoding="utf-8"?>
+<preference-headers
+    xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <header
+        android:fragment="com.android.seandroid_manager.SEAndroidManager$SELinuxEnforcingFragment"
+        android:title="@string/selinux_enforcing_fragment_title">
+    </header>
+
+    <header
+        android:fragment="com.android.seandroid_manager.SEAndroidManager$SELinuxBooleanFragment"
+        android:title="@string/selinux_boolean_fragment_title">
+    </header>
+
+</preference-headers>

File res/xml/seandroid_manager_headers.xml

-<?xml version="1.0" encoding="utf-8"?>
-<preference-headers
-    xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <header
-        android:fragment="com.android.seandroid_manager.SEAndroidManager$SELinuxEnforcingFragment"
-        android:title="@string/selinux_enforcing_fragment_title">
-    </header>
-
-    <header
-        android:fragment="com.android.seandroid_manager.SEAndroidManager$SELinuxBooleanFragment"
-        android:title="@string/selinux_boolean_fragment_title">
-    </header>
-
-</preference-headers>

File src/com/android/seandroid_manager/RestoreSettings.java

             if (enforcing != null) {
                 SELinux.setSELinuxEnforce(enforcing.contentEquals("1") ? true : false);
             }
-       
+
             // Restore SELinux boolean settings
             final String booleanList = Settings.Secure.getString(mContentResolver,
                                                                  Settings.Secure.SELINUX_BOOLEANS);

File src/com/android/seandroid_manager/SEAndroidManager.java

 import java.util.List;
 
 public class SEAndroidManager extends PreferenceActivity {
+
     @Override
     public void onBuildHeaders(List<Header> target) {
-        loadHeadersFromResource(R.xml.seandroid_manager_headers, target);
+        if (SELinux.isSELinuxEnabled()) {
+            loadHeadersFromResource(R.xml.enabled_headers, target);
+        } else {
+            loadHeadersFromResource(R.xml.disabled_headers, target);
+        }
+    }
+
+    public static class SELinuxDisabledFragment extends PreferenceFragment {
+
+        @Override
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+
+            addPreferencesFromResource(R.xml.selinux_not_enabled);
+        }
     }
 
     public static class SELinuxEnforcingFragment extends PreferenceFragment {
             if (preference == mSELinuxToggleEnforce) {
                 SELinux.setSELinuxEnforce(!SELinux.isSELinuxEnforced());
                 mSELinuxToggleEnforce.setChecked(SELinux.isSELinuxEnforced());
+                saveEnforcing();
             }
             return true;
         }
+
+        private void saveEnforcing()
+        {
+            String enforcing = SELinux.isSELinuxEnforced() ? "1" : "0";
+            Settings.Secure.putString(getActivity().getContentResolver(),
+                                      Settings.Secure.SELINUX_ENFORCING,
+                                      enforcing);
+        }
     }
 
     public static class SELinuxBooleanFragment extends ListFragment {
 
-        private static final String PREF_FILE = "seandroid_settings";
         private SharedPreferences mPrefs;
         private myBooleanAdapter mAdapter;
 
                             String name = (String)holder.tx.getText();
                             SELinux.setBooleanValue(name, isChecked);
                             holder.cb.setChecked(SELinux.getBooleanValue(name));
+                            saveBooleans();
                         }
                     });
                 return convertView;
             }
 
-            class ViewHolder {
+            private class ViewHolder {
                 TextView tx;
                 CheckBox cb;
             }
+
+            private void saveBooleans()
+            {
+                String[] mNames = SELinux.getBooleanNames();
+                StringBuilder newBooleanList = new StringBuilder();
+                boolean first = true;
+                for (String n : mNames) {
+                    if (first) {
+                        first = false;
+                    } else {
+                        newBooleanList.append(",");
+                    }
+                    newBooleanList.append(n);
+                    newBooleanList.append(":");
+                    newBooleanList.append(SELinux.getBooleanValue(n) ? 1 : 0);
+                }
+                Settings.Secure.putString(getActivity().getContentResolver(),
+                                          Settings.Secure.SELINUX_BOOLEANS,
+                                          newBooleanList.toString());
+            }
+        }
+
+        @Override
+        public void onActivityCreated(Bundle savedInstanceState) {
+            super.onActivityCreated(savedInstanceState);
+            setEmptyText(getActivity().getText(R.string.selinux_no_booleans));
         }
 
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
-            mAdapter = new myBooleanAdapter(getActivity(), R.layout.selinux_manage_booleans, 
+            mAdapter = new myBooleanAdapter(getActivity(), R.layout.selinux_manage_booleans,
                                             SELinux.getBooleanNames());
             setListAdapter(mAdapter);
         }

File src/com/android/seandroid_manager/SaveSettings.java

-package com.android.seandroid_manager;
-
-import android.content.ContentResolver;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.SELinux;
-import android.provider.Settings;
-import android.util.Log;
-
-public class SaveSettings extends BroadcastReceiver {
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        if (SELinux.isSELinuxEnabled()) {
-            Log.d("SEAndroidManager", "Saving the SELinux settings");
-
-            ContentResolver mContentResolver = context.getContentResolver();
-
-            // Save SELinux enforcing mode
-            String enforcing = SELinux.isSELinuxEnforced() ? "1" : "0";
-            Settings.Secure.putString(mContentResolver, Settings.Secure.SELINUX_ENFORCING, 
-                                      enforcing);
-
-            // Save SELinux boolean settings
-            String[] mNames = SELinux.getBooleanNames();
-            StringBuilder newBooleanList = new StringBuilder();
-            boolean first = true;
-            for (String n : mNames) {
-                if (first) {
-                    first = false;
-                } else {
-                    newBooleanList.append(",");
-                } 
-                newBooleanList.append(n);
-                newBooleanList.append(":");
-                newBooleanList.append(SELinux.getBooleanValue(n) ? 1 : 0);
-            }
-            Settings.Secure.putString(mContentResolver, Settings.Secure.SELINUX_BOOLEANS, 
-                                      newBooleanList.toString());
-        }
-    }
-}