1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.messaging.datamodel.action;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import com.android.messaging.sms.SmsReleaseStorage;
23import com.android.messaging.util.Assert;
24
25/**
26 * Action used to handle low storage related issues on the device.
27 */
28public class HandleLowStorageAction extends Action implements Parcelable {
29    private static final int SUB_OP_CODE_CLEAR_MEDIA_MESSAGES = 100;
30    private static final int SUB_OP_CODE_CLEAR_OLD_MESSAGES = 101;
31
32    public static void handleDeleteMediaMessages(final long durationInMillis) {
33        final HandleLowStorageAction action = new HandleLowStorageAction(
34                SUB_OP_CODE_CLEAR_MEDIA_MESSAGES, durationInMillis);
35        action.start();
36    }
37
38    public static void handleDeleteOldMessages(final long durationInMillis) {
39        final HandleLowStorageAction action = new HandleLowStorageAction(
40                SUB_OP_CODE_CLEAR_OLD_MESSAGES, durationInMillis);
41        action.start();
42    }
43
44    private static final String KEY_SUB_OP_CODE = "sub_op_code";
45    private static final String KEY_CUTOFF_DURATION_MILLIS = "cutoff_duration_millis";
46
47    private HandleLowStorageAction(final int subOpcode, final long durationInMillis) {
48        super();
49        actionParameters.putInt(KEY_SUB_OP_CODE, subOpcode);
50        actionParameters.putLong(KEY_CUTOFF_DURATION_MILLIS, durationInMillis);
51    }
52
53    @Override
54    protected Object executeAction() {
55        final int subOpCode = actionParameters.getInt(KEY_SUB_OP_CODE);
56        final long durationInMillis = actionParameters.getLong(KEY_CUTOFF_DURATION_MILLIS);
57        switch (subOpCode) {
58            case SUB_OP_CODE_CLEAR_MEDIA_MESSAGES:
59                SmsReleaseStorage.deleteMessages(0, durationInMillis);
60                break;
61
62            case SUB_OP_CODE_CLEAR_OLD_MESSAGES:
63                SmsReleaseStorage.deleteMessages(1, durationInMillis);
64                break;
65
66            default:
67                Assert.fail("Unsupported action type!");
68                break;
69        }
70        return true;
71    }
72
73    private HandleLowStorageAction(final Parcel in) {
74        super(in);
75    }
76
77    public static final Parcelable.Creator<HandleLowStorageAction> CREATOR
78            = new Parcelable.Creator<HandleLowStorageAction>() {
79        @Override
80        public HandleLowStorageAction createFromParcel(final Parcel in) {
81            return new HandleLowStorageAction(in);
82        }
83
84        @Override
85        public HandleLowStorageAction[] newArray(final int size) {
86            return new HandleLowStorageAction[size];
87        }
88    };
89
90    @Override
91    public void writeToParcel(final Parcel parcel, final int flags) {
92        writeActionToParcel(parcel, flags);
93    }
94}
95