Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007-2008 Esmertec AG.
      3  * Copyright (C) 2007-2008 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.im.app;
     19 
     20 import android.app.Activity;
     21 import android.app.AlertDialog;
     22 import android.content.res.Resources;
     23 import android.os.Handler;
     24 import android.os.Looper;
     25 import android.os.Message;
     26 import android.widget.Toast;
     27 
     28 import com.android.im.R;
     29 import com.android.im.engine.Contact;
     30 import com.android.im.engine.ContactListListener;
     31 import com.android.im.engine.ImErrorInfo;
     32 
     33 public class SimpleAlertHandler extends Handler {
     34 
     35     Activity mActivity;
     36     Resources mRes;
     37 
     38     public SimpleAlertHandler(Activity activity) {
     39         mActivity = activity;
     40         mRes = mActivity.getResources();
     41     }
     42 
     43     protected void promptDisconnectedEvent(Message msg) {
     44         long providerId = ((long)msg.arg1 << 32) | msg.arg2;
     45         ImApp app = ImApp.getApplication(mActivity);
     46         ProviderDef provider = app.getProvider(providerId);
     47         ImErrorInfo error = (ImErrorInfo) msg.obj;
     48         String promptMsg;
     49         if (error != null) {
     50             promptMsg = mActivity.getString(R.string.signed_out_prompt_with_error,
     51                     provider.mName, ErrorResUtils.getErrorRes(mRes, error.getCode()));
     52         } else {
     53             promptMsg = mActivity.getString(R.string.signed_out_prompt, provider.mName);
     54         }
     55         Toast.makeText(mActivity, promptMsg, Toast.LENGTH_SHORT).show();
     56     }
     57 
     58     public void registerForBroadcastEvents() {
     59          ImApp.getApplication(mActivity).registerForBroadcastEvent(
     60                 ImApp.EVENT_CONNECTION_DISCONNECTED,
     61                 this);
     62     }
     63 
     64     public void unregisterForBroadcastEvents() {
     65         ImApp.getApplication(mActivity).unregisterForBroadcastEvent(
     66                 ImApp.EVENT_CONNECTION_DISCONNECTED,
     67                 this);
     68     }
     69 
     70     public void showAlert(int titleId, int messageId) {
     71         showAlert(mRes.getString(titleId), mRes.getString(messageId));
     72     }
     73 
     74     public void showAlert(int titleId, CharSequence message) {
     75         showAlert(mRes.getString(titleId), message);
     76     }
     77 
     78     public void showAlert(CharSequence title, int messageId) {
     79         showAlert(title, mRes.getString(messageId));
     80     }
     81 
     82     public void showAlert(final CharSequence title, final CharSequence message) {
     83         if (Looper.myLooper() == getLooper()) {
     84             new AlertDialog.Builder(mActivity)
     85                     .setTitle(title)
     86                     .setMessage(message)
     87                     .setPositiveButton(R.string.ok, null)
     88                     .show();
     89         } else {
     90             post(new Runnable() {
     91                 public void run() {
     92                     new AlertDialog.Builder(mActivity)
     93                             .setTitle(title)
     94                             .setMessage(message)
     95                             .setPositiveButton(R.string.ok, null)
     96                             .show();
     97                 }
     98             });
     99         }
    100     }
    101 
    102     public void showServiceErrorAlert() {
    103         showAlert(R.string.error, R.string.service_error);
    104     }
    105 
    106     public void showContactError(int errorType, ImErrorInfo error,
    107             String listName, Contact contact) {
    108         int id = 0;
    109         switch (errorType) {
    110         case ContactListListener.ERROR_LOADING_LIST:
    111             id = R.string.load_contact_list_failed;
    112             break;
    113 
    114         case ContactListListener.ERROR_CREATING_LIST:
    115             id = R.string.add_list_failed;
    116             break;
    117 
    118         case ContactListListener.ERROR_BLOCKING_CONTACT:
    119             id = R.string.block_contact_failed;
    120             break;
    121 
    122         case ContactListListener.ERROR_UNBLOCKING_CONTACT:
    123             id = R.string.unblock_contact_failed;
    124             break;
    125         }
    126 
    127         String errorInfo = ErrorResUtils.getErrorRes(mRes, error.getCode());
    128         if (id != 0) {
    129             errorInfo = mRes.getText(id) + "\n" + errorInfo;
    130         }
    131 
    132         showAlert(R.string.error, errorInfo);
    133     }
    134 
    135 }
    136