Add an automatic storage management job service.

This service runs once a day when plugged in when the device has
under 15% free space remaining. If the FeatureFactory has a
storage management job, it runs the job to begin to free up space
on the device.

This is a temporary landing place and will be refactored very
quickly out of Settings.

Bug: 28600825

Change-Id: Id2ebb42a333b3b4e3daef4e50cf985fe055b85c7
This commit is contained in:
Daniel Nishi
2016-05-04 10:08:36 -07:00
parent 74cc9772e9
commit 80c204420b
9 changed files with 237 additions and 2 deletions

View File

@@ -2971,6 +2971,22 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
<!-- Automatic storage management tasks. -->
<service
android:name=".deletionhelper.AutomaticStorageManagementJobService"
android:label="@string/automatic_storage_manager_service_label"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="@bool/enable_automatic_storage_management"
android:exported="false"/>
<receiver android:name=".deletionhelper.AutomaticStorageBroadcastReceiver"
android:enabled="@bool/enable_automatic_storage_management">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<!-- This is the longest AndroidManifest.xml ever. -->
</application>
</manifest>

View File

@@ -43,4 +43,7 @@
<!--Whether the storage manager exists. -->
<bool name="config_has_storage_manager">false</bool>
<!-- Whether the automatic storage management job should be scheduled. -->
<bool name="enable_automatic_storage_management">false</bool>
</resources>

View File

@@ -7649,4 +7649,7 @@
<!-- Preference title for the automatic storage manager toggle. [CHAR LIMIT=60]-->
<string name="automatic_storage_manager_preference_title">Storage manager</string>
<!-- Automatic storage management service label. [CHAR LIMIT=40]-->
<string name="automatic_storage_manager_service_label">Automatic Storage Management Service</string>
</resources>

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 The Android Open Source 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.deletionhelper;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
/**
* A {@link BroadcastReceiver} listening for {@link Intent#ACTION_BOOT_COMPLETED} broadcasts to
* schedule an automatic storage management job. Automatic storage management jobs are only
* scheduled once a day for a plugged in device.
*/
public class AutomaticStorageBroadcastReceiver extends BroadcastReceiver {
private static final int AUTOMATIC_STORAGE_JOB_ID = 0;
private static final long PERIOD = DateUtils.DAY_IN_MILLIS;
@Override
public void onReceive(Context context, Intent intent) {
JobScheduler jobScheduler =
(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
ComponentName component = new ComponentName(context,
AutomaticStorageManagementJobService.class);
JobInfo job = new JobInfo.Builder(AUTOMATIC_STORAGE_JOB_ID, component)
.setRequiresCharging(true)
.setRequiresDeviceIdle(true)
.setPeriodic(PERIOD)
.build();
jobScheduler.schedule(job);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2016 The Android Open Source 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.deletionhelper;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.provider.Settings;
import android.util.Log;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.overlay.StorageManagementJobProvider;
import java.io.File;
/**
* {@link JobService} class to start automatic storage clearing jobs to free up space. The job only
* starts if the device is under a certain percent of free storage.
*/
public class AutomaticStorageManagementJobService extends JobService {
private static final String TAG = "AsmJobService";
private static final String SHARED_PREFRENCES_NAME = "automatic_storage_manager_settings";
private static final String KEY_DAYS_TO_RETAIN = "days_to_retain";
private static final long DEFAULT_LOW_FREE_PERCENT = 15;
private StorageManagementJobProvider mProvider;
@Override
public boolean onStartJob(JobParameters args) {
boolean isEnabled =
Settings.Secure.getInt(getContentResolver(),
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
if (!isEnabled) {
return false;
}
StorageManager manager = getSystemService(StorageManager.class);
VolumeInfo internalVolume = manager.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
final File dataPath = internalVolume.getPath();
if (!volumeNeedsManagement(dataPath)) {
Log.i(TAG, "Skipping automatic storage management.");
return false;
}
mProvider = FeatureFactory.getFactory(this).getStorageManagementJobProvider();
if (mProvider != null) {
return mProvider.onStartJob(this, args, getDaysToRetain());
}
return false;
}
@Override
public boolean onStopJob(JobParameters args) {
if (mProvider != null) {
return mProvider.onStopJob(this, args);
}
return false;
}
private int getDaysToRetain() {
SharedPreferences sharedPreferences =
getSharedPreferences(SHARED_PREFRENCES_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getInt(KEY_DAYS_TO_RETAIN,
AutomaticStorageManagerSettings.DEFAULT_DAYS_TO_RETAIN);
}
private boolean volumeNeedsManagement(final File dataPath) {
long lowStorageThreshold = (dataPath.getTotalSpace() * DEFAULT_LOW_FREE_PERCENT) / 100;
return dataPath.getFreeSpace() < lowStorageThreshold;
}
}

View File

@@ -17,6 +17,9 @@
package com.android.settings.deletionhelper;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
@@ -41,9 +44,13 @@ import com.android.settings.widget.SwitchBar.OnSwitchChangeListener;
*/
public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment implements
OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
public static final int DEFAULT_DAYS_TO_RETAIN = 90;
private static final String SHARED_PREFRENCES_NAME = "automatic_storage_manager_settings";
private static final String KEY_DAYS = "days";
private static final String KEY_DELETION_HELPER = "deletion_helper";
private static final String KEY_STORAGE_MANAGER_SWITCH = "storage_manager_active";
private static final String KEY_DAYS_TO_RETAIN = "days_to_retain";
private DropDownPreference mDaysToRetain;
private Preference mDeletionHelper;
@@ -70,6 +77,14 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, 0) != 0;
mStorageManagerSwitch.setChecked(isChecked);
mStorageManagerSwitch.setOnPreferenceChangeListener(this);
SharedPreferences sharedPreferences =
getContext().getSharedPreferences(SHARED_PREFRENCES_NAME,
Context.MODE_PRIVATE);
int value = sharedPreferences.getInt(KEY_DAYS_TO_RETAIN, DEFAULT_DAYS_TO_RETAIN);
String[] stringValues =
getResources().getStringArray(R.array.automatic_storage_management_days_values);
mDaysToRetain.setValue(stringValues[daysValueToIndex(value, stringValues)]);
}
@Override
@@ -88,8 +103,11 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
Settings.Secure.AUTOMATIC_STORAGE_MANAGER_ENABLED, checked ? 1 : 0);
break;
case KEY_DAYS:
// TODO: Configure a setting which controls how many days of data the storage manager
// should retain.
SharedPreferences.Editor editor =
getContext().getSharedPreferences(SHARED_PREFRENCES_NAME,
Context.MODE_PRIVATE).edit();
editor.putInt(KEY_DAYS_TO_RETAIN, Integer.parseInt((String) newValue));
editor.apply();
break;
}
return true;
@@ -108,4 +126,14 @@ public class AutomaticStorageManagerSettings extends SettingsPreferenceFragment
}
return true;
}
private static int daysValueToIndex(int value, String[] indices) {
for (int i = 0; i < indices.length; i++) {
int thisValue = Integer.parseInt(indices[i]);
if (value == thisValue) {
return i;
}
}
return indices.length - 1;
}
}

View File

@@ -65,6 +65,7 @@ public abstract class FeatureFactory {
* Return a provider which adds additional deletion services to the Deletion Helper.
*/
public abstract DeletionHelperFeatureProvider getDeletionHelperFeatureProvider();
public abstract StorageManagementJobProvider getStorageManagementJobProvider();
public static final class FactoryNotFoundException extends RuntimeException {
public FactoryNotFoundException(Throwable throwable) {

View File

@@ -33,4 +33,9 @@ public final class FeatureFactoryImpl extends FeatureFactory {
return null;
}
@Override
public StorageManagementJobProvider getStorageManagementJobProvider() {
return null;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2016 The Android Open Source 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.overlay;
import android.app.job.JobParameters;
import android.content.Context;
/**
* Feature provider for automatic storage management jobs.
*/
public interface StorageManagementJobProvider {
/**
* Starts an asynchronous deletion job to clear out storage older than
* @param params Standard JobService parameters.
* @param daysToRetain Number of days of information to retain on the device.
* @return If the job needs to process the work on a separate thread.
*/
boolean onStartJob(Context context, JobParameters params, int daysToRetain);
/**
* Attempt to stop the execution of the job.
* @param params Parameters specifying info about this job.
* @return If the job should be rescheduled.
*/
boolean onStopJob(Context context, JobParameters params);
}