OSDN Git Service

DataUsageUtils: Clean up based on code review
authorTyler Dunn <ireallylikeshrek@gmail.com>
Tue, 1 Nov 2016 03:37:36 +0000 (23:37 -0400)
committerTyler Dunn <ireallylikeshrek@gmail.com>
Fri, 25 Nov 2016 06:47:04 +0000 (22:47 -0800)
* Add missing header
* Extract DataUsageContract.UID + " = ? " into a string
* Simplify "0" string
* Use try-with-resource block to ensure cursor is closed

This commit is based on the post-merge feedback of Kamaljeet Maini,
Fabrice Di Meglio and Danny Baumann.

Change-Id: I774948ba119e3e4a2dd438e1ed1588bfb9fefae2

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) {