CALLINFO登録画面で、個人の登録時に傾けると落ちるのを修正しました。
[callbackmailservice:callbackmailservice-h.git] / src / jp / co / kayo / android / activity / CallInfoEditorGroup.java
1 package jp.co.kayo.android.activity;
2 /***
3  * Copyright (c) 2009-2010 Re:Kayo-System, Ltd. All rights reserved.
4  * 
5  *      This program is free software; you can redistribute it and/or modify it under
6  *      the terms of the GNU General Public License as published by the Free Software
7  *      Foundation; either version 2 of the License, or (at your option) any later
8  *      version.
9  *      
10  *      This program is distributed in the hope that it will be useful, but WITHOUT
11  *      ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  *      FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  *      details.
14  *      
15  *      You should have received a copy of the GNU General Public License along with
16  *      this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  *      Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import jp.co.kayo.android.util.DatabaseHelper;
26 import jp.co.kayo.android.util.StringUtil;
27 import android.app.AlertDialog;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.database.Cursor;
32 import android.graphics.Color;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.provider.BaseColumns;
36 import android.provider.Contacts.People;
37 import android.provider.Contacts.Phones;
38 import android.view.KeyEvent;
39 import android.view.Menu;
40 import android.view.MenuItem;
41 import android.view.View;
42 import android.view.View.OnClickListener;
43 import android.view.View.OnFocusChangeListener;
44 import android.view.View.OnKeyListener;
45 import android.view.inputmethod.InputMethodManager;
46 import android.widget.AdapterView;
47 import android.widget.ArrayAdapter;
48 import android.widget.Button;
49 import android.widget.EditText;
50 import android.widget.ImageButton;
51 import android.widget.LinearLayout;
52 import android.widget.ListView;
53 import android.widget.RadioGroup;
54 import android.widget.Spinner;
55 import android.widget.Toast;
56 import android.widget.AdapterView.OnItemSelectedListener;
57 import android.widget.RadioGroup.OnCheckedChangeListener;
58
59 /**
60  * 許可電話登録画面 TEST
61  * 
62  * @author naoki
63  */
64 public class CallInfoEditorGroup extends MyThemeListActivity implements OnClickListener, OnItemSelectedListener,
65                 OnCheckedChangeListener, OnFocusChangeListener, OnKeyListener {
66
67         @Override
68         protected int getLayoutID() {
69                 return R.layout.callinfo_editor_group;
70         }
71
72         private static final String TAG = "CallInfoEditorGroup";
73
74         /** 登録タイプのキー */
75         public static final String REGIST_TYPE = "REGIST_TYPE";
76         /** 登録タイプ個人の値 */
77         public static final String REGIST_TYPE_SINGLE = "SINGLE";
78         /** 登録タイプグループの値 */
79         public static final String REGIST_TYPE_GROUP = "GROUP";
80
81         // 電話番号クリック
82         private static final int REQUEST_CODE_CALLINFO_PICK = 0;
83         // テンプレート編集
84         private static final int REQUEST_CODE_TEMPLATE_EDIT = 1;
85         // テンプレート追加
86         private static final int REQUEST_CODE_TEMPLATE_INSERT = 2;
87         // メンバー追加
88         private static final int REQUEST_CODE_CLICK_ADD_MEMBER = 3;
89         // 
90         private static final int REQUEST_CODE_SINGLE_CONFIRM = 4;
91         
92         private static final int REQUEST_CODE_GROUP_CONFIRM = 5;
93
94         // テンプレート一覧
95         private List<TemplateInfo> mTemplateList = null;
96
97         // グループ登録用CallInfo
98         private List<GroupInfo> mGroupInfoList = null;
99
100         // 個人登録用CallInfo
101         private CallInfo mCallInfo;
102         // テンプレートIDの無効値
103         private static final int NEW_TEMPLATE = Integer.MIN_VALUE;
104         // テンプレート一覧
105         private Spinner mSpinner = null;
106         // 連絡先タイプのパネル
107         private LinearLayout mllRegistType;
108         // 連絡先
109         private EditText mEtxtUserName;
110         // グループ名
111         private EditText mEtxtGroupName;
112
113         private List<CallInfo> mTempCallInfoList;
114
115         // 個人/グループ
116         private RadioGroup mRadioGroup;
117
118         // 連絡先タイプ(SINGLE/GROUP)
119         private String mAddressType;
120         // ACTIONタイプ(EDIT/INSERT)
121         private String mAction;
122         // 新規:null 更新:CallInfo._id
123         private Long mId = null;
124         
125
126         @Override
127         protected void onCreate(Bundle savedInstanceState) {
128                 super.onCreate(savedInstanceState);
129
130                 getListView().setBackgroundColor(Color.TRANSPARENT);
131                 getListView().setCacheColorHint(Color.TRANSPARENT);
132
133                 // パラメータ取得
134
135                 // ACTION
136                 if(mAction == null)
137                         mAction = getIntent().getAction();
138
139                 // 連絡先タイプ(個人/グループ)
140                 if(mAddressType == null)
141                         mAddressType = getIntent().getExtras().getString(REGIST_TYPE);
142
143                 // CALLINFO._id
144                 if(mId == null)
145                         mId = (Long) getIntent().getExtras().get(BaseColumns._ID);
146
147                 // 画面の初期化
148                 init();
149         }
150
151         /**
152          * 画面の初期化
153          */
154         private void init() {
155
156                 // タイトル
157                 initTitle();
158
159                 // 個人/グループ
160                 initRbxRegistType();
161
162                 // (グ)メンバー追加ボタン
163                 ImageButton btnAdd = (ImageButton) findViewById(R.id.btnAddMember);
164                 btnAdd.setOnClickListener(this);
165
166                 Button btnOK = (Button) findViewById(R.id.btn_done);
167                 btnOK.setOnClickListener(this);
168
169                 Button btnCancel = (Button) findViewById(R.id.btn_cancel);
170                 btnCancel.setOnClickListener(this);
171
172                 DatabaseHelper dh = null;
173                 try {
174                         dh = new DatabaseHelper(this);
175
176                         mCallInfo = new CallInfo();
177                         if (isActionEdit()) {
178
179                                 // DB値をセット
180                                 // IDに紐つくユーザーを取得
181                                 List<CallInfo> list = dh.getCallInfoList(BaseColumns._ID + "=" + mId);
182                                 if (list != null && list.size() > 0) {
183                                         mCallInfo = list.get(0);
184                                 }
185                         }
186                         
187                         // (個)連絡先
188                         initEtxtAddress(dh);
189
190                         // (グ)グループ名
191                         initEtxtGroup(dh);
192
193                         // (グ)メンバーリスト
194                         initLstMemberList(dh);
195
196                         // テンプレート一覧
197                         initSpnTemplate(dh);
198
199                 } finally {
200                         if (dh != null)
201                                 dh.close();
202                 }
203         }
204
205         /**
206          * 連絡先タイプが個人ならtrue
207          * 
208          * @return
209          */
210         private boolean isRegistModeSingle() {
211
212                 return REGIST_TYPE_SINGLE.equals(mAddressType);
213         }
214
215         /**
216          * 連絡先タイプがグループならtrue
217          * 
218          * @return
219          */
220         private boolean isRegistModeGroup() {
221
222                 return REGIST_TYPE_GROUP.equals(mAddressType);
223         }
224
225         /**
226          * ACTION_INSERTならtrue
227          * 
228          * @return
229          */
230         boolean isActionInsert() {
231
232                 return Intent.ACTION_INSERT.equals(mAction);
233         }
234
235         /**
236          * ACTION_EDITならtrue
237          * 
238          * @return
239          */
240         boolean isActionEdit() {
241
242                 return Intent.ACTION_EDIT.equals(mAction);
243         }
244
245         /**
246          * 画面タイトルの初期化
247          */
248         private void initTitle() {
249
250                 if (isActionEdit()) {
251                         setTitle(getText(R.string.title_edit_call_number));
252                 } else {
253                         setTitle(getText(R.string.title_create_call_number));
254                 }
255
256         }
257
258         /**
259          * 連絡先の初期化
260          * 
261          * @param dh
262          */
263         private void initEtxtAddress(DatabaseHelper dh) {
264
265                 if(isRegistModeSingle()){
266                         mEtxtUserName = (EditText) findViewById(R.id.callNumberRegist);
267                         if(isActionInsert()){
268                                 // 変更可
269                                 mEtxtUserName.setOnClickListener(this);
270                                 mEtxtUserName.setOnKeyListener(this);
271                         }
272                 }
273         }
274
275         /**
276          * (グ)グループ名の初期化
277          * 
278          * @param dh
279          */
280         private void initEtxtGroup(DatabaseHelper dh) {
281
282                 mEtxtGroupName = (EditText) findViewById(R.id.groupName);
283                 mEtxtGroupName.setOnKeyListener(this);
284                 if (isActionEdit() && isRegistModeGroup()) {
285
286                         // DB値をセット
287                         mEtxtGroupName.setText(mCallInfo.getCALLNO());
288                         
289                 } else if(isActionInsert()){
290                         
291                         mEtxtGroupName.setText(R.string.new_group);
292                         mEtxtGroupName.setOnFocusChangeListener(this);
293                 }
294         }
295
296         /**
297          * (グ)メンバー一覧の初期化
298          * 
299          * @param dh
300          */
301         private void initLstMemberList(DatabaseHelper dh) {
302
303                 mGroupInfoList = new ArrayList<GroupInfo>();
304                 if (isActionEdit() && isRegistModeGroup()) {
305                         // 変更、かつ、グループモードの場合
306
307                         // グループIDで検索した結果を初期表示とする
308                         List<CallInfo> list = dh.getCallInfoList(BaseColumns._ID + "=" + mId);
309                         if (list != null && list.size() > 0) {
310                                 mGroupInfoList = dh.getGroupInfoList(DatabaseHelper.GROUP_FIELD_GROUPID + "=\""
311                                                 + list.get(0).getCALLNO() + "\"");
312                         }
313
314                         initListView();
315                 }
316         }
317
318         /**
319          * 連絡先タイプの初期化
320          */
321         private void initRbxRegistType() {
322
323                 mRadioGroup = (RadioGroup) findViewById(R.id.RadioGroupCallInfo);
324                 if (isActionEdit()) {
325                         // 編集モードの場合は非表示
326                         mllRegistType = (LinearLayout) findViewById(R.id.RegistType);
327                         mllRegistType.setVisibility(View.GONE);
328                         setModeGroup();
329
330                 } else {
331
332                         mRadioGroup.setOnCheckedChangeListener(this);
333
334                         if (isRegistModeSingle()) {
335                                 setModeSingle();
336                         } else {
337                                 setModeGroup();
338                         }
339                 }
340         }
341
342         /**
343          * テンプレート一覧の初期化
344          * 
345          * @param dh
346          */
347         private void initSpnTemplate(DatabaseHelper dh) {
348
349                 initSpn(dh);
350
351                 if (isActionEdit()) {
352                         // DB値をセット
353
354                         int templateNo = mCallInfo.getTEMPNO();
355                         for (int i = 0; i < mTemplateList.size(); i++) {
356                                 TemplateInfo templateInfo = mTemplateList.get(i);
357                                 if (templateInfo.getID() == mCallInfo.getTEMPNO()) {
358                                         templateNo = mCallInfo.getTEMPNO();
359                                         break;
360                                 }
361                         }
362                         mSpinner.setSelection(templateNo);
363
364                 }
365         }
366
367         /**
368          * テンプレート一覧の初期化<br>
369          * DBからテンプレートを全て取得して、最後に新規テンプレートを追加する。
370          * 
371          * @param dh
372          */
373         private void initSpn(DatabaseHelper dh) {
374
375                 // DBより一覧取得
376                 mTemplateList = dh.getTemplateInfoList(null);
377
378                 // リストの最後に「新規テンプレート」を追加
379                 TemplateInfo addInfo = new TemplateInfo();
380                 addInfo.setNAME(getString(R.string.spn_new_template));
381                 addInfo.setId(NEW_TEMPLATE);
382                 mTemplateList.add(addInfo);
383
384                 ArrayAdapter<TemplateInfo> arrayAdapter = new ArrayAdapter<TemplateInfo>(this, R.layout.simple_spinner_item,
385                                 mTemplateList);
386                 arrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
387                 mSpinner = (Spinner) findViewById(R.id.TemplateType);
388                 mSpinner.setAdapter(arrayAdapter);
389                 mSpinner.setOnItemSelectedListener(this);
390         }
391
392         @Override
393         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
394
395                 if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CALLINFO_PICK) {
396                         // (個)連絡先の場合
397
398                         Address address = new Address(data);
399
400                         EditText etxtAddress = (EditText) findViewById(R.id.callNumberRegist);
401                         if (!StringUtil.isNullOrSpace(address.getNumber())) {
402                                 // 電話番号が入っている場合
403                                 mCallInfo.setCALLNO(address.getName());
404                                 etxtAddress.setText(address.getName());
405                                 return;
406                         } else {
407                                 etxtAddress.setText("");
408                                 mCallInfo.setCALLNO(null);
409
410                                 Toast toast = Toast.makeText(this, getString(R.string.msg_template_007), Toast.LENGTH_SHORT);
411                                 toast.show();
412                         }
413
414                 } else if (resultCode == RESULT_OK && data != null
415                                 && (requestCode == REQUEST_CODE_TEMPLATE_INSERT || requestCode == REQUEST_CODE_TEMPLATE_EDIT)) {
416                         // テンプレート編集画面(INSERT/EDIT)から戻ってきた場合
417                         DatabaseHelper dh = null;
418                         try {
419                                 dh = new DatabaseHelper(this);
420
421                                 // テンプレート一覧の再表示
422                                 initSpn(dh);
423
424                                 for (int i = 0; i < mTemplateList.size(); i++) {
425                                         TemplateInfo ti = (TemplateInfo) mTemplateList.get(i);
426                                         if ((Integer) data.getExtras().get(BaseColumns._ID) == ti.getID()) {
427                                                 mSpinner.setSelection(i);
428                                                 break;
429                                         }
430                                 }
431                         } finally {
432                                 if (dh != null)
433                                         dh.close();
434                         }
435                 } else if (resultCode == RESULT_OK && data != null && requestCode == REQUEST_CODE_CLICK_ADD_MEMBER) {
436                         // メンバー追加ボタン押下結果の場合
437
438                         Address address = new Address(data);
439                         if (StringUtil.isNullOrSpace(address.getNumber())) {
440                                 // 電話番号が無い場合
441                                 Toast toast = Toast.makeText(this, getString(R.string.msg_template_007), Toast.LENGTH_SHORT);
442                                 toast.show();
443                         } else {
444                                 // 重複チェック
445                                 for (GroupInfo info : mGroupInfoList) {
446                                         if (info.getName().equals(address.getName())) {
447
448                                                 showAlert(getString(R.string.err_input010));
449                                                 return;
450                                         }
451                                 }
452
453                                 GroupInfo info = new GroupInfo();
454                                 info.setName(address.getName());
455                                 mGroupInfoList.add(info);
456
457                                 initListView();
458                         }
459                 } else if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_SINGLE_CONFIRM) {
460                         
461                         // (個)確認ダイアログを表示した場合
462                         this.updateSingle();
463                         
464                 } else if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_GROUP_CONFIRM) {
465                         
466                         // (グ)確認ダイアログを表示した場合
467                         this.updateGroup();
468
469                 }
470         }
471
472         private void updateSingle() {
473                 DatabaseHelper dh = null;
474                 try {
475                         dh = new DatabaseHelper(this);
476                         mCallInfo.setTEMPNO(mSpinner.getSelectedItemPosition());
477                         mCallInfo.setENABLED(true);
478                         if (mTempCallInfoList.size() > 0)
479                                 dh.deleteMember(mTempCallInfoList.get(0));
480                         dh.insert(mCallInfo);
481                         
482                         Toast toast = Toast.makeText(this, String.format(getString(R.string.warn_input3), mCallInfo.getCALLNO()),
483                                         Toast.LENGTH_SHORT);
484                         toast.show();
485                         setResult(RESULT_OK);
486                         finish();
487                 } finally {
488                         if (dh != null)
489                                 dh.close();
490                 }
491         }
492         
493         private void updateGroup() {
494
495                 // グループ登録
496                 DatabaseHelper dh = null;
497                 try {
498                         dh = new DatabaseHelper(this);
499
500                         // CallInfo
501                         CallInfo callInfo = new CallInfo();
502                         callInfo.setCALLNO(mEtxtGroupName.getText().toString());
503                         callInfo.setTEMPNO(mSpinner.getSelectedItemPosition());
504                         // グループフラグを立てる
505                         callInfo.setGROUPNO(true);
506                         callInfo.setENABLED(true);
507
508                         if (isActionEdit()) {
509                                 dh.update(callInfo, BaseColumns._ID + "=" + mId);
510                         } else {
511                                 dh.insert(callInfo);
512                         }
513
514                         // Groupメンバーの登録
515                         for (GroupInfo groupInfo : mGroupInfoList) {
516                                 
517                                 // DELETE
518                                 dh.deleteGroupInfo(groupInfo, DatabaseHelper.GROUP_FIELD_NAME + "=\"" + groupInfo.getName() + "\"");
519                                 
520                                 // INSERT
521                                 GroupInfo info = new GroupInfo();
522                                 info.setGroupid(mEtxtGroupName.getText().toString());
523                                 info.setName(groupInfo.getName());
524                                 dh.insert(info);
525                         }
526                         
527                         
528                         
529
530                         Toast toast = Toast.makeText(this, String.format(getString(R.string.warn_input3), mEtxtGroupName.getText()
531                                         .toString()), Toast.LENGTH_SHORT);
532                         toast.show();
533                         setResult(RESULT_OK);
534                         finish();
535
536                 } finally {
537                         if (dh != null)
538                                 dh.close();
539                 }
540         }
541
542         /**
543          * ダイアログ表示
544          * 
545          * @param msg
546          */
547         void showAlert(String msg) {
548
549                 AlertDialog.Builder ad = new AlertDialog.Builder(this);
550                 ad.setIcon(R.drawable.icon48);
551                 ad.setMessage(msg);
552                 ad.setNegativeButton(R.string.button_ok, new DialogInterface.OnClickListener() {
553                         @Override
554                         public void onClick(DialogInterface dialog, int which) {
555                                 dialog.cancel();
556                         }
557                 });
558                 ad.show();
559         }
560
561         @Override
562         public boolean onCreateOptionsMenu(Menu menu) {
563                 // メニューアイテムを追加します
564                 menu.add(Menu.NONE, MenuMain.HELP_ID, Menu.NONE, R.string.menu_help);
565                 return super.onCreateOptionsMenu(menu);
566         }
567
568         @Override
569         public boolean onOptionsItemSelected(MenuItem item) {
570                 switch (item.getItemId()) {
571                 case MenuMain.HELP_ID:
572                         startActivityForResult(new Intent(this, HelpActivity.class), 0);
573                         break;
574                 }
575
576                 return super.onOptionsItemSelected(item);
577         }
578
579         @Override
580         public void onClick(View v) {
581
582                 if (R.id.callNumberRegist == v.getId() || R.id.btnAddMember == v.getId()) {
583                         // (個)連絡先選択、もしくは(グ)メンバー追加ボタン押下時
584
585                         // Uri lookupUri = People.CONTENT_URI;
586                         // Intent intent = new Intent(Intent.ACTION_PICK, lookupUri);
587                         Intent intent = new Intent(this, PeopleListActivity.class);
588
589                         intent.setAction(Intent.ACTION_PICK);
590                         if (R.id.callNumberRegist == v.getId()) {
591                                 // (個)連絡先選択
592                                 startActivityForResult(intent, REQUEST_CODE_CALLINFO_PICK);
593                         } else if (R.id.btnAddMember == v.getId()) {
594                                 // (グ)メンバー追加ボタン押下
595                                 startActivityForResult(intent, REQUEST_CODE_CLICK_ADD_MEMBER);
596                         }
597
598                 } else if (R.id.btn_done == v.getId()) {
599                         // DONEボタン押下時
600
601                         RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroupCallInfo);
602                         if (radioGroup.getCheckedRadioButtonId() == R.id.RadioButtonSingle) {
603                                 
604                                 // 個人登録
605                                 if (StringUtil.isEmpty(mCallInfo.getCALLNO())) {
606
607                                         // ダイアログ表示
608                                         showAlert(getString(R.string.err_input001));
609                                         return;
610                                 }
611
612                                 // 新規テンプレートならダイアログ表示
613                                 if (mTemplateList.get((int) mSpinner.getSelectedItemId()).getID() == Integer.MIN_VALUE) {
614                                         showAlert(getString(R.string.err_input012));
615                                         return;
616                                 }
617
618                                 // 上書き確認
619                                 CallInfo ci = null;
620                                 DatabaseHelper dh = null;
621                                 try {
622                                         dh = new DatabaseHelper(this);
623
624                                         // 重複チェック
625                                         mTempCallInfoList = dh.getCallInfoList(DatabaseHelper.CALLINF_FIELD_CALLNO + " = '"
626                                                         + mCallInfo.getCALLNO() + "'");
627
628                                         if (mTempCallInfoList.size() > 0) {
629                                                 // 上書き確認が必要な場合
630
631                                                 ci = mTempCallInfoList.get(0);
632                                                 Intent intent = new Intent(this, CallInfoConfirmDialog.class);
633                                                 Bundle b = new Bundle();
634                                                 String[] person = new String[] { ci.getCALLNO() };
635                                                 b.putStringArray("PERSON", person);
636                                                 intent.putExtra("BUNDLE", b);
637                                                 startActivityForResult(intent, REQUEST_CODE_SINGLE_CONFIRM);
638
639                                         } else {
640                                                 // 上書きが必要ではない場合
641                                                 updateSingle();
642                                         }
643                                 } finally {
644                                         if (dh != null)
645                                                 dh.close();
646                                 }
647
648                         } else if (radioGroup.getCheckedRadioButtonId() == R.id.RadioButtonGroup) {
649
650                                 // グループ登録
651
652                                 // グループ名 必須入力チェック
653                                 String groupName = mEtxtGroupName.getText().toString();
654                                 if (StringUtil.isEmpty(groupName)) {
655
656                                         // ダイアログ表示
657                                         showAlert(getString(R.string.err_input008));
658                                         return;
659                                 }
660
661                                 if (mGroupInfoList == null || mGroupInfoList.size() == 0) {
662                                         // メンバー数チェック
663                                         // ダイアログ表示
664                                         showAlert(getString(R.string.err_input011));
665                                         return;
666                                 }
667
668                                 // 新規テンプレートならダイアログ表示
669                                 if (mTemplateList.get((int) mSpinner.getSelectedItemId()).getID() == Integer.MIN_VALUE) {
670                                         showAlert(getString(R.string.err_input012));
671                                         return;
672                                 }
673
674                                 DatabaseHelper dh = null;
675                                 try {
676                                         dh = new DatabaseHelper(this);
677                                         
678                                         // グループ名の重複チェック
679                                         List<CallInfo> callinfoList = dh.getCallInfoList(DatabaseHelper.CALLINF_FIELD_CALLNO + "=\""
680                                                         + groupName + "\"");
681                                         if (isActionInsert() && callinfoList.size() > 0) {
682                                                 showAlert(getString(R.string.err_input009));
683                                                 return;
684                                         }
685
686                                         // 入力チェック
687                                         List<GroupInfo> list = new ArrayList<GroupInfo>();
688                                         for (GroupInfo info : mGroupInfoList) {
689                                                 // 必須入力 名前
690                                                 if (info.getName() == null || info.getName().length() == 0) {
691
692                                                         // ダイアログ表示
693                                                         showAlert(getString(R.string.err_input001));
694                                                         return;
695                                                 }
696
697                                                 // 既に登録されているレコードがある場合
698                                                 String whereClause = DatabaseHelper.GROUP_FIELD_NAME + "=\"" + info.getName() + "\"";
699                                                 if (isActionEdit()) {
700                                                         // 更新の場合は、更新対象のIDは除く
701                                                         whereClause += " AND " + DatabaseHelper.GROUP_FIELD_GROUPID + "!=\""
702                                                                         + mCallInfo.getCALLNO() + "\"";
703                                                 }
704                                                 list.addAll(dh.getGroupInfoList(whereClause));
705                                         }
706
707                                         if (list != null && list.size() > 0) {
708                                                 // 上書き確認が必要な場合
709                                                 Intent intent = new Intent(this, CallInfoConfirmDialog.class);
710                                                 Bundle b = new Bundle();
711                                                 String[] person = new String[list.size()];
712                                                 for (int i = 0; i < list.size(); i++) {
713                                                         person[i] = list.get(i).getName();
714                                                 }
715                                                 b.putStringArray("PERSON", person);
716                                                 intent.putExtra("BUNDLE", b);
717                                                 startActivityForResult(intent, REQUEST_CODE_GROUP_CONFIRM);
718
719                                         } else {
720                                                 
721                                                 // 上書きが必要ではない場合
722                                                 this.updateGroup();
723                                                 
724                                         }
725
726                                 } finally {
727                                         if (dh != null)
728                                                 dh.close();
729                                 }
730                                 
731                         }
732                 } else if (R.id.btn_cancel == v.getId()) {
733                         // キャンセル
734                         finish();
735                 }
736         }
737
738         @Override
739         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
740
741                 if (mTemplateList.get(position).getID() == NEW_TEMPLATE) {
742                         // テンプレート追加画面表示
743                         Intent intent = new Intent(Intent.ACTION_INSERT, null, CallInfoEditorGroup.this, TemplateEditor.class);
744                         startActivityForResult(intent, REQUEST_CODE_TEMPLATE_INSERT);
745                 }
746         }
747
748         @Override
749         public void onNothingSelected(AdapterView<?> arg0) {
750         }
751
752         @Override
753         public void onCheckedChanged(RadioGroup group, int checkedId) {
754                 if (R.id.RadioButtonSingle == checkedId) {
755                         // ラジオボタン(個人)
756                         setModeSingle();
757
758                 } else if (R.id.RadioButtonGroup == checkedId) {
759                         // ラジオボタン(グループ)
760
761                         setModeGroup();
762                 }
763         }
764
765         /**
766          * シングル表示に切り替えます。
767          * 
768          * @param
769          */
770         private void setModeSingle() {
771
772                 mRadioGroup.check(R.id.RadioButtonSingle);
773                 LinearLayout llSingle = (LinearLayout) findViewById(R.id.LinearLayoutSingle);
774                 llSingle.setVisibility(View.VISIBLE);
775                 LinearLayout llGroup = (LinearLayout) findViewById(R.id.LinearLayouyGroup);
776                 llGroup.setVisibility(View.GONE);
777                 LinearLayout llMember = (LinearLayout) findViewById(R.id.LinearLayoutMember);
778                 llMember.setVisibility(View.GONE);
779         }
780
781         /**
782          * グループ表示に切り替えます。
783          * 
784          * @param
785          */
786         private void setModeGroup() {
787
788                 mRadioGroup.check(R.id.RadioButtonGroup);
789                 LinearLayout llSingle = (LinearLayout) findViewById(R.id.LinearLayoutSingle);
790                 llSingle.setVisibility(View.GONE);
791                 LinearLayout llGroup = (LinearLayout) findViewById(R.id.LinearLayouyGroup);
792                 llGroup.setVisibility(View.VISIBLE);
793                 LinearLayout llMember = (LinearLayout) findViewById(R.id.LinearLayoutMember);
794                 llMember.setVisibility(View.VISIBLE);
795         }
796
797         @Override
798         protected void onListItemClick(ListView l, View v, int position, long id) {
799
800                 // メンバーを削除しますか?
801                 final int pos = position;
802                 final Context context = CallInfoEditorGroup.this;
803                 AlertDialog.Builder ad = new AlertDialog.Builder(context);
804                 ad.setMessage(String.format(context.getString(R.string.msg_template_008), mGroupInfoList.get(pos).getName()));
805                 ad.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
806                         public void onClick(DialogInterface dialog, int whichButton) {
807                                 mGroupInfoList.remove(pos);
808                                 initListView();
809                         }
810                 });
811                 ad.setNegativeButton(context.getString(R.string.btn_no), new DialogInterface.OnClickListener() {
812                         @Override
813                         public void onClick(DialogInterface dialog, int which) {
814                                 dialog.cancel();
815                         }
816                 });
817                 ad.show();
818         }
819
820         /**
821          * メンバーリストの更新
822          */
823         private void initListView() {
824                 Collections.sort(mGroupInfoList);
825                 ArrayAdapter<GroupInfo> adapter = new ArrayAdapter<GroupInfo>(this, R.layout.member_row, mGroupInfoList);
826                 setListAdapter(adapter);
827         }
828
829         @Override
830         public long getSelectedItemId() {
831                 // 選択したポジションのCALLINFOからIDを取得する
832                 return mGroupInfoList.get(getSelectedItemPosition()).getID();
833         }
834
835         /**
836          * 連絡先
837          * 
838          * @author naoki
839          */
840         private class Address {
841
842                 private long mId;
843
844                 private String mName;
845
846                 private String mNumber;
847
848                 private long mPrimaryEmailId;
849
850                 public Address(Intent data) {
851
852                         Cursor personCursor = managedQuery(data.getData(), new String[] { People.NUMBER }, null, null, null);
853                         if (personCursor.moveToFirst()) {
854
855                                 mNumber = personCursor.getString(personCursor.getColumnIndexOrThrow(People.NUMBER));
856                                 Uri lookupUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(mNumber));
857                                 String[] projection = new String[] { People._ID, People.NAME, People.NUMBER, People.PRIMARY_EMAIL_ID };
858                                 Cursor cur = getContentResolver().query(lookupUri, projection, null, null, null);
859                                 if (cur != null && cur.moveToFirst()) {
860
861                                         mId = cur.getLong(cur.getColumnIndex(People._ID));
862                                         mName = cur.getString(cur.getColumnIndex(People.NAME));
863                                         mPrimaryEmailId = cur.getLong(cur.getColumnIndex(People.PRIMARY_EMAIL_ID));
864                                 }
865                         }
866                 }
867
868                 public long getId() {
869                         return mId;
870                 }
871
872                 public String getName() {
873                         return mName;
874                 }
875
876                 public String getNumber() {
877                         return mNumber;
878                 }
879
880                 public void setNumber(String number) {
881                         mNumber = number;
882                 }
883
884                 public long getPrimaryEmailId() {
885                         return mPrimaryEmailId;
886                 }
887         }
888
889         @Override
890         public void onFocusChange(View v, boolean hasFocus) {
891                 
892                 if (R.id.groupName == v.getId()) {
893                         // グループ
894                         if (getString(R.string.new_group).equals(mEtxtGroupName.getText().toString())) {
895                                 // 文字列が「新しいグループ」の場合はブランクにする
896                                 mEtxtGroupName.setText("");
897                         }
898                 }
899         }
900         
901         @Override
902         protected void onRestoreInstanceState(Bundle state) {
903                 super.onRestoreInstanceState(state);
904                 
905                 mId = state.getLong("ID");
906                 
907                 mCallInfo.setCALLNO(state.getString("CALLNO"));
908                 
909                 if(mGroupInfoList != null && mGroupInfoList.size() == 0){
910                         Bundle b = state.getBundle("BUNDLE");
911                         if(b != null){
912                                 String[] person = b.getStringArray("PERSON");
913                                 for(String name : person){
914                                         GroupInfo info = new GroupInfo();
915                                         info.setName(name);
916                                         mGroupInfoList.add(info);
917                                 }
918                                 initListView();
919                         }
920                 }
921                 
922         }
923
924         @Override
925         protected void onSaveInstanceState(Bundle outState) {
926
927                 if (mId != null) {
928                         outState.putInt("ID", mId.intValue());
929                 }
930
931                 if (mCallInfo != null) {
932                         outState.putString("CALLNO", mCallInfo.getCALLNO());
933                 }
934                 
935                 if(mEtxtGroupName != null){
936                         outState.putString("GROUPNAME", mEtxtGroupName.getText().toString());
937                 }
938                 
939                 if(mGroupInfoList != null && mGroupInfoList.size() > 0){
940                         Bundle b = new Bundle();
941                         String[] person = new String[mGroupInfoList.size()];
942                         for (int i = 0; i < mGroupInfoList.size(); i++) {
943                                 person[i] = mGroupInfoList.get(i).getName();
944                         }
945                         b.putStringArray("PERSON", person);
946                         outState.putBundle("BUNDLE", b);
947                 }
948                 
949                 super.onSaveInstanceState(outState);
950         }
951
952         @Override
953         public boolean onKey(View v, int keyCode, KeyEvent event) {
954                 if (v instanceof EditText && event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
955                         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
956                         imm.hideSoftInputFromWindow(mEtxtGroupName.getWindowToken(), 0);
957                         return true;
958                 }
959                 return false;
960         }
961
962 }