Home | History | Annotate | Download | only in action
      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 
     17 package com.android.messaging.datamodel.action;
     18 
     19 import android.content.ContentValues;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 import com.android.messaging.datamodel.BugleDatabaseOperations;
     24 import com.android.messaging.datamodel.DataModel;
     25 import com.android.messaging.datamodel.DatabaseHelper;
     26 import com.android.messaging.datamodel.DatabaseWrapper;
     27 import com.android.messaging.datamodel.DatabaseHelper.PartColumns;
     28 import com.android.messaging.util.Assert;
     29 
     30 /**
     31  * Action used to update size fields of a single part
     32  */
     33 public class UpdateMessagePartSizeAction extends Action implements Parcelable {
     34     /**
     35      * Update size of part
     36      */
     37     public static void updateSize(final String partId, final int width, final int height) {
     38         Assert.notNull(partId);
     39         Assert.inRange(width, 0, Integer.MAX_VALUE);
     40         Assert.inRange(height, 0, Integer.MAX_VALUE);
     41 
     42         final UpdateMessagePartSizeAction action = new UpdateMessagePartSizeAction(
     43                 partId, width, height);
     44         action.start();
     45     }
     46 
     47     private static final String KEY_PART_ID = "part_id";
     48     private static final String KEY_WIDTH = "width";
     49     private static final String KEY_HEIGHT = "height";
     50 
     51     private UpdateMessagePartSizeAction(final String partId, final int width, final int height) {
     52         actionParameters.putString(KEY_PART_ID, partId);
     53         actionParameters.putInt(KEY_WIDTH, width);
     54         actionParameters.putInt(KEY_HEIGHT, height);
     55     }
     56 
     57     @Override
     58     protected Object executeAction() {
     59         final String partId = actionParameters.getString(KEY_PART_ID);
     60         final int width = actionParameters.getInt(KEY_WIDTH);
     61         final int height = actionParameters.getInt(KEY_HEIGHT);
     62 
     63         final DatabaseWrapper db = DataModel.get().getDatabase();
     64         db.beginTransaction();
     65         try {
     66             final ContentValues values = new ContentValues();
     67 
     68             values.put(PartColumns.WIDTH, width);
     69             values.put(PartColumns.HEIGHT, height);
     70 
     71             // Part may have been deleted so allow update to fail without asserting
     72             BugleDatabaseOperations.updateRowIfExists(db, DatabaseHelper.PARTS_TABLE,
     73                     PartColumns._ID, partId, values);
     74 
     75             db.setTransactionSuccessful();
     76         } finally {
     77             db.endTransaction();
     78         }
     79         return null;
     80     }
     81 
     82     private UpdateMessagePartSizeAction(final Parcel in) {
     83         super(in);
     84     }
     85 
     86     public static final Parcelable.Creator<UpdateMessagePartSizeAction> CREATOR
     87             = new Parcelable.Creator<UpdateMessagePartSizeAction>() {
     88         @Override
     89         public UpdateMessagePartSizeAction createFromParcel(final Parcel in) {
     90             return new UpdateMessagePartSizeAction(in);
     91         }
     92 
     93         @Override
     94         public UpdateMessagePartSizeAction[] newArray(final int size) {
     95             return new UpdateMessagePartSizeAction[size];
     96         }
     97     };
     98 
     99     @Override
    100     public void writeToParcel(final Parcel parcel, final int flags) {
    101         writeActionToParcel(parcel, flags);
    102     }
    103 }
    104