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.ConversationColumns;
     26 import com.android.messaging.datamodel.DatabaseWrapper;
     27 import com.android.messaging.datamodel.MessagingContentProvider;
     28 import com.android.messaging.util.Assert;
     29 
     30 /**
     31  * Action used to update conversation options such as notification settings.
     32  */
     33 public class UpdateConversationOptionsAction extends Action
     34         implements Parcelable {
     35     /**
     36      * Enable/disable conversation notifications.
     37      */
     38     public static void enableConversationNotifications(final String conversationId,
     39             final boolean enableNotification) {
     40         Assert.notNull(conversationId);
     41 
     42         final UpdateConversationOptionsAction action = new UpdateConversationOptionsAction(
     43                 conversationId, enableNotification, null, null);
     44         action.start();
     45     }
     46 
     47     /**
     48      * Sets conversation notification sound.
     49      */
     50     public static void setConversationNotificationSound(final String conversationId,
     51             final String ringtoneUri) {
     52         Assert.notNull(conversationId);
     53 
     54         final UpdateConversationOptionsAction action = new UpdateConversationOptionsAction(
     55                 conversationId, null, ringtoneUri, null);
     56         action.start();
     57     }
     58 
     59     /**
     60      * Enable/disable vibrations for conversation notification.
     61      */
     62     public static void enableVibrationForConversationNotification(final String conversationId,
     63             final boolean enableVibration) {
     64         Assert.notNull(conversationId);
     65 
     66         final UpdateConversationOptionsAction action = new UpdateConversationOptionsAction(
     67                 conversationId, null, null, enableVibration);
     68         action.start();
     69     }
     70 
     71     private static final String KEY_CONVERSATION_ID = "conversation_id";
     72 
     73     // Keys for all settable settings.
     74     private static final String KEY_ENABLE_NOTIFICATION = "enable_notification";
     75     private static final String KEY_RINGTONE_URI = "ringtone_uri";
     76     private static final String KEY_ENABLE_VIBRATION = "enable_vibration";
     77 
     78     protected UpdateConversationOptionsAction(final String conversationId,
     79             final Boolean enableNotification, final String ringtoneUri,
     80             final Boolean enableVibration) {
     81         Assert.notNull(conversationId);
     82         actionParameters.putString(KEY_CONVERSATION_ID, conversationId);
     83         if (enableNotification != null) {
     84             actionParameters.putBoolean(KEY_ENABLE_NOTIFICATION, enableNotification);
     85         }
     86 
     87         if (ringtoneUri != null) {
     88             actionParameters.putString(KEY_RINGTONE_URI, ringtoneUri);
     89         }
     90 
     91         if (enableVibration != null) {
     92             actionParameters.putBoolean(KEY_ENABLE_VIBRATION, enableVibration);
     93         }
     94     }
     95 
     96     protected void putOptionValuesInTransaction(final ContentValues values,
     97             final DatabaseWrapper dbWrapper) {
     98         Assert.isTrue(dbWrapper.getDatabase().inTransaction());
     99         if (actionParameters.containsKey(KEY_ENABLE_NOTIFICATION)) {
    100             values.put(ConversationColumns.NOTIFICATION_ENABLED,
    101                     actionParameters.getBoolean(KEY_ENABLE_NOTIFICATION));
    102         }
    103 
    104         if (actionParameters.containsKey(KEY_RINGTONE_URI)) {
    105             values.put(ConversationColumns.NOTIFICATION_SOUND_URI,
    106                     actionParameters.getString(KEY_RINGTONE_URI));
    107         }
    108 
    109         if (actionParameters.containsKey(KEY_ENABLE_VIBRATION)) {
    110             values.put(ConversationColumns.NOTIFICATION_VIBRATION,
    111                     actionParameters.getBoolean(KEY_ENABLE_VIBRATION));
    112         }
    113     }
    114 
    115     @Override
    116     protected Object executeAction() {
    117         final String conversationId = actionParameters.getString(KEY_CONVERSATION_ID);
    118 
    119         final DatabaseWrapper db = DataModel.get().getDatabase();
    120         db.beginTransaction();
    121         try {
    122             final ContentValues values = new ContentValues();
    123             putOptionValuesInTransaction(values, db);
    124 
    125             BugleDatabaseOperations.updateConversationRowIfExists(db, conversationId, values);
    126 
    127             db.setTransactionSuccessful();
    128         } finally {
    129             db.endTransaction();
    130         }
    131         MessagingContentProvider.notifyConversationMetadataChanged(conversationId);
    132         return null;
    133     }
    134 
    135     protected UpdateConversationOptionsAction(final Parcel in) {
    136         super(in);
    137     }
    138 
    139     public static final Parcelable.Creator<UpdateConversationOptionsAction> CREATOR
    140             = new Parcelable.Creator<UpdateConversationOptionsAction>() {
    141         @Override
    142         public UpdateConversationOptionsAction createFromParcel(final Parcel in) {
    143             return new UpdateConversationOptionsAction(in);
    144         }
    145 
    146         @Override
    147         public UpdateConversationOptionsAction[] newArray(final int size) {
    148             return new UpdateConversationOptionsAction[size];
    149         }
    150     };
    151 
    152     @Override
    153     public void writeToParcel(final Parcel parcel, final int flags) {
    154         writeActionToParcel(parcel, flags);
    155     }
    156 }
    157