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.AlertDialog; 21 import android.app.ListActivity; 22 import android.content.ContentUris; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.content.res.Resources; 27 import android.database.Cursor; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.RemoteException; 31 import android.util.Log; 32 import android.view.View; 33 import android.view.Window; 34 import android.widget.ListAdapter; 35 import android.widget.ListView; 36 import android.widget.ResourceCursorAdapter; 37 38 import com.android.im.IContactListManager; 39 import com.android.im.IImConnection; 40 import com.android.im.R; 41 import com.android.im.plugin.BrandingResourceIDs; 42 import com.android.im.provider.Imps; 43 44 public class BlockedContactsActivity extends ListActivity { 45 ImApp mApp; 46 SimpleAlertHandler mHandler; 47 48 private static final String[] PROJECTION = { 49 Imps.BlockedList._ID, 50 Imps.BlockedList.ACCOUNT, 51 Imps.BlockedList.PROVIDER, 52 Imps.BlockedList.NICKNAME, 53 Imps.BlockedList.USERNAME, 54 Imps.BlockedList.AVATAR_DATA, 55 }; 56 57 static final int ACCOUNT_COLUMN = 1; 58 static final int PROVIDER_COLUMN = 2; 59 static final int NICKNAME_COLUMN = 3; 60 static final int USERNAME_COLUMN = 4; 61 static final int AVATAR_COLUMN = 5; 62 63 @Override onCreate(Bundle icicle)64 protected void onCreate(Bundle icicle) { 65 super.onCreate(icicle); 66 67 requestWindowFeature(Window.FEATURE_LEFT_ICON); 68 69 setContentView(R.layout.blocked_contacts_activity); 70 mHandler = new SimpleAlertHandler(this); 71 72 mApp = ImApp.getApplication(this); 73 mApp.startImServiceIfNeed(); 74 if (!resolveIntent()) { 75 finish(); 76 return; 77 } 78 } 79 80 @Override onListItemClick(ListView l, View v, int position, long id)81 protected void onListItemClick(ListView l, View v, int position, long id) { 82 Cursor c = (Cursor) l.getAdapter().getItem(position); 83 if (c == null) { 84 mHandler.showAlert(R.string.error, R.string.select_contact); 85 return; 86 } 87 long providerId = c.getLong(PROVIDER_COLUMN); 88 String username = c.getString(USERNAME_COLUMN); 89 String nickname = c.getString(NICKNAME_COLUMN); 90 mApp.callWhenServiceConnected(mHandler, new UnblockAction(providerId, username, nickname)); 91 } 92 resolveIntent()93 private boolean resolveIntent() { 94 Intent i = getIntent(); 95 Uri uri = i.getData(); 96 if (uri == null) { 97 warning("No data to show"); 98 return false; 99 } 100 101 long accountId = ContentUris.parseId(uri); 102 Uri accountUri = ContentUris.withAppendedId(Imps.Account.CONTENT_URI, accountId); 103 Cursor accountCursor = getContentResolver().query(accountUri, null, null, null, null); 104 if (accountCursor == null) { 105 warning("Bad account"); 106 return false; 107 } 108 if (!accountCursor.moveToFirst()) { 109 warning("Bad account"); 110 accountCursor.close(); 111 return false; 112 } 113 114 long providerId = accountCursor.getLong( 115 accountCursor.getColumnIndexOrThrow(Imps.Account.PROVIDER)); 116 String username = accountCursor.getString( 117 accountCursor.getColumnIndexOrThrow(Imps.Account.USERNAME)); 118 119 BrandingResources brandingRes = mApp.getBrandingResource(providerId); 120 getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, 121 brandingRes.getDrawable(BrandingResourceIDs.DRAWABLE_LOGO)); 122 123 setTitle(getResources().getString(R.string.blocked_list_title, username)); 124 accountCursor.close(); 125 126 Cursor c = managedQuery(uri, PROJECTION, null, Imps.BlockedList.DEFAULT_SORT_ORDER); 127 if (c == null) { 128 warning("Database error when query " + uri); 129 return false; 130 } 131 132 ListAdapter adapter = new BlockedContactsAdapter(c, this); 133 setListAdapter(adapter); 134 return true; 135 } 136 warning(String msg)137 private static void warning(String msg) { 138 Log.w(ImApp.LOG_TAG, "<BlockContactsActivity> " + msg); 139 } 140 141 private static class BlockedContactsAdapter extends ResourceCursorAdapter { BlockedContactsAdapter(Cursor c, Context context)142 public BlockedContactsAdapter(Cursor c, Context context) { 143 super(context, R.layout.blocked_contact_view, c); 144 } 145 146 @Override bindView(View view, Context context, Cursor cursor)147 public void bindView(View view, Context context, Cursor cursor) { 148 if (view instanceof BlockedContactView) { 149 ((BlockedContactView) view).bind(cursor); 150 } 151 } 152 } 153 154 private class UnblockAction implements Runnable { 155 private long mProviderId; 156 String mUserName; 157 private String mNickName; 158 UnblockAction(long providerId, String userName, String nickName)159 public UnblockAction(long providerId, String userName, String nickName) { 160 mProviderId = providerId; 161 mUserName = userName; 162 mNickName = nickName; 163 } 164 run()165 public void run() { 166 final IImConnection conn = mApp.getConnection(mProviderId); 167 if (conn == null) { 168 mHandler.showAlert(R.string.error, R.string.disconnected); 169 return; 170 } 171 DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){ 172 public void onClick(DialogInterface dialog, int whichButton) { 173 try { 174 IContactListManager manager = conn.getContactListManager(); 175 manager.unBlockContact(mUserName); 176 } catch (RemoteException e) { 177 mHandler.showServiceErrorAlert(); 178 } 179 } 180 }; 181 Resources r = getResources(); 182 183 new AlertDialog.Builder(BlockedContactsActivity.this) 184 .setTitle(R.string.confirm) 185 .setMessage(r.getString(R.string.confirm_unblock_contact, mNickName)) 186 .setPositiveButton(R.string.yes, confirmListener) // default button 187 .setNegativeButton(R.string.no, null) 188 .setCancelable(false) 189 .show(); 190 } 191 192 } 193 } 194