OSDN Git Service

DataUsageUtils: Clean up based on code review
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DataUsageUtils.java
index 5f08067..09b6b10 100644 (file)
@@ -1,5 +1,20 @@
-package com.android.settings;
+/*
+ * Copyright (C) 2016 The CyanogenMod project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
+package com.android.settings;
 
 import android.app.AlarmManager;
 import android.app.PendingIntent;
@@ -11,12 +26,12 @@ import android.util.Log;
 
 import cyanogenmod.providers.DataUsageContract;
 
-
 /**
  * This class contains utility helper functions for accessing DataUsageProvider
  */
 public class DataUsageUtils {
     private static final String TAG = DataUsageUtils.class.getSimpleName();
+    private static final String WHERE_CLAUSE = DataUsageContract.UID + " = ? ";
     private static final int DATAUSAGE_SERVICE_ALARM_ID = 0x102030;
     private static boolean DEBUG = true;
 
@@ -42,8 +57,8 @@ public class DataUsageUtils {
         }
         context.getContentResolver().delete(
                 DataUsageContract.CONTENT_URI,
-                DataUsageContract.UID + " = ? ",
-                new String [] { String.valueOf(uid)}
+                WHERE_CLAUSE,
+                new String [] { String.valueOf(uid) }
         );
     }
 
@@ -66,43 +81,36 @@ public class DataUsageUtils {
         context.getContentResolver().update(
                 DataUsageContract.CONTENT_URI,
                 values,
-                DataUsageContract.UID + " = ? ",
-                new String [] { String.valueOf(uid)}
+                WHERE_CLAUSE,
+                new String [] { String.valueOf(uid) }
         );
     }
 
     public static boolean isDbEnabled(Context context) {
         boolean dbEnabled = false;
-        Cursor cursor = context.getContentResolver().query(
+        try (Cursor cursor = context.getContentResolver().query(
                 DataUsageContract.CONTENT_URI,
                 null,
-                DataUsageContract.UID + " = ? ",
-                new String [] { String.valueOf("0") },
-                null
-        );
-
-        if (cursor != null) {
-            cursor.close();
+                WHERE_CLAUSE,
+                new String [] { "0" },
+                null)) {
             dbEnabled = true;
         }
         return dbEnabled;
     }
 
-
     public static boolean isAppEnabled(Context context, int uid) {
         boolean appEnabled = false;
-        Cursor cursor = context.getContentResolver().query(
+
+        try (Cursor cursor = context.getContentResolver().query(
                 DataUsageContract.CONTENT_URI,
                 null,
-                DataUsageContract.UID + " = ? ",
+                WHERE_CLAUSE,
                 new String [] { String.valueOf(uid) },
-                null
-        );
-        if (cursor != null) {
+                null)) {
             if (cursor.moveToFirst()) {
                 appEnabled = cursor.getInt(DataUsageContract.COLUMN_OF_ENABLE) == 1;
             }
-            cursor.close();
         }
 
         if (DEBUG) {