Ignore:
Timestamp:
Mar 22, 2015 10:22:44 AM (9 years ago)
Author:
nanang
Message:

Misc (re #1782): Coding style works (indentation, etc) on Android pjsua2 sample app codes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/swig/java/android/src/org/pjsip/pjsua2/app/MainActivity.java

    r4997 r5017  
    4444import org.pjsip.pjsua2.*; 
    4545 
    46 public class MainActivity extends Activity implements Handler.Callback, MyAppObserver { 
    47         public static MyApp app = null; 
    48         public static MyCall currentCall = null; 
    49         public static MyAccount account = null; 
    50         public static AccountConfig accCfg = null; 
    51          
    52         private ListView buddyListView; 
    53         private SimpleAdapter buddyListAdapter; 
    54         private int buddyListSelectedIdx = -1; 
     46public class MainActivity extends Activity 
     47                          implements Handler.Callback, MyAppObserver 
     48{ 
     49    public static MyApp app = null; 
     50    public static MyCall currentCall = null; 
     51    public static MyAccount account = null; 
     52    public static AccountConfig accCfg = null; 
     53 
     54    private ListView buddyListView; 
     55    private SimpleAdapter buddyListAdapter; 
     56    private int buddyListSelectedIdx = -1; 
    5557    ArrayList<Map<String, String>> buddyList; 
    5658    private String lastRegStatus = ""; 
    5759 
    58         private final Handler handler = new Handler(this); 
    59         public class MSG_TYPE { 
    60                 public final static int INCOMING_CALL = 1; 
    61                 public final static int CALL_STATE = 2; 
    62                 public final static int REG_STATE = 3; 
    63                 public final static int BUDDY_STATE = 4; 
    64                 public final static int CALL_MEDIA_STATE = 5; 
    65         } 
    66  
    67         private HashMap<String, String> putData(String uri, String status) { 
    68                 HashMap<String, String> item = new HashMap<String, String>(); 
    69                 item.put("uri", uri); 
    70                 item.put("status", status); 
    71                 return item; 
    72         } 
    73          
    74         private void showCallActivity() { 
    75         Intent intent = new Intent(this, CallActivity.class); 
    76         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    77         startActivity(intent); 
    78         } 
    79  
    80         @Override 
    81         protected void onCreate(Bundle savedInstanceState) { 
    82                 super.onCreate(savedInstanceState); 
    83                 setContentView(R.layout.activity_main); 
    84                  
    85                 if (app == null) { 
    86                         app = new MyApp(); 
    87                         /* Wait for GDB to init */ 
    88                         if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { 
    89                                 try { 
    90                                         Thread.sleep(5000); 
    91                                 } catch (InterruptedException e) {} 
     60    private final Handler handler = new Handler(this); 
     61    public class MSG_TYPE 
     62    { 
     63        public final static int INCOMING_CALL = 1; 
     64        public final static int CALL_STATE = 2; 
     65        public final static int REG_STATE = 3; 
     66        public final static int BUDDY_STATE = 4; 
     67        public final static int CALL_MEDIA_STATE = 5; 
     68    } 
     69 
     70    private HashMap<String, String> putData(String uri, String status) 
     71    { 
     72        HashMap<String, String> item = new HashMap<String, String>(); 
     73        item.put("uri", uri); 
     74        item.put("status", status); 
     75        return item; 
     76    } 
     77 
     78    private void showCallActivity() 
     79    { 
     80        Intent intent = new Intent(this, CallActivity.class); 
     81        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     82        startActivity(intent); 
     83    } 
     84 
     85    @Override 
     86    protected void onCreate(Bundle savedInstanceState) 
     87    { 
     88        super.onCreate(savedInstanceState); 
     89        setContentView(R.layout.activity_main); 
     90 
     91        if (app == null) { 
     92            app = new MyApp(); 
     93            // Wait for GDB to init, for native debugging only 
     94            if (false && 
     95                (getApplicationInfo().flags &  
     96                ApplicationInfo.FLAG_DEBUGGABLE) != 0) 
     97            { 
     98                try { 
     99                    Thread.sleep(5000); 
     100                } catch (InterruptedException e) {} 
     101            } 
     102 
     103            app.init(this, getFilesDir().getAbsolutePath()); 
     104        } 
     105 
     106        if (app.accList.size() == 0) { 
     107            accCfg = new AccountConfig(); 
     108            accCfg.setIdUri("sip:localhost"); 
     109            accCfg.getNatConfig().setIceEnabled(true); 
     110            account = app.addAcc(accCfg); 
     111        } else { 
     112            account = app.accList.get(0); 
     113            accCfg = account.cfg; 
     114        } 
     115 
     116        buddyList = new ArrayList<Map<String, String>>(); 
     117        for (int i = 0; i < account.buddyList.size(); i++) { 
     118            buddyList.add(putData(account.buddyList.get(i).cfg.getUri(), 
     119                                  account.buddyList.get(i).getStatusText())); 
     120        } 
     121 
     122        String[] from = { "uri", "status" }; 
     123        int[] to = { android.R.id.text1, android.R.id.text2 }; 
     124        buddyListAdapter = new SimpleAdapter( 
     125                                        this, buddyList, 
     126                                        android.R.layout.simple_list_item_2, 
     127                                        from, to); 
     128 
     129        buddyListView = (ListView) findViewById(R.id.listViewBuddy);; 
     130        buddyListView.setAdapter(buddyListAdapter); 
     131        buddyListView.setOnItemClickListener( 
     132            new AdapterView.OnItemClickListener() 
     133            { 
     134                @Override 
     135                public void onItemClick(AdapterView<?> parent, 
     136                                        final View view, 
     137                                        int position, long id)  
     138                { 
     139                    view.setSelected(true); 
     140                    buddyListSelectedIdx = position; 
     141                } 
     142            } 
     143        ); 
     144 
     145    } 
     146 
     147    @Override 
     148    public boolean onCreateOptionsMenu(Menu menu) 
     149    { 
     150        // Inflate the menu; this adds items to the action bar 
     151        // if it is present. 
     152        getMenuInflater().inflate(R.menu.main, menu); 
     153        return true; 
     154    } 
     155 
     156    @Override 
     157    public boolean onOptionsItemSelected(MenuItem item) 
     158    { 
     159        switch (item.getItemId()) { 
     160            case R.id.action_acc_config: 
     161                dlgAccountSetting(); 
     162                break; 
     163 
     164            case R.id.action_quit: 
     165                Message m = Message.obtain(handler, 0); 
     166                m.sendToTarget(); 
     167                break; 
     168 
     169            default: 
     170                break; 
     171        } 
     172 
     173        return true; 
     174    }    
     175 
     176    @Override 
     177    public boolean handleMessage(Message m) 
     178    { 
     179        if (m.what == 0) { 
     180 
     181            app.deinit(); 
     182            finish(); 
     183            Runtime.getRuntime().gc(); 
     184            android.os.Process.killProcess(android.os.Process.myPid()); 
     185 
     186        } else if (m.what == MSG_TYPE.CALL_STATE) { 
     187 
     188            CallInfo ci = (CallInfo) m.obj; 
     189 
     190            /* Forward the message to CallActivity */ 
     191            if (CallActivity.handler_ != null) { 
     192                Message m2 = Message.obtain(CallActivity.handler_, 
     193                    MSG_TYPE.CALL_STATE, ci); 
     194                m2.sendToTarget(); 
     195            } 
     196 
     197        } else if (m.what == MSG_TYPE.CALL_MEDIA_STATE) { 
     198 
     199            /* Forward the message to CallActivity */ 
     200            if (CallActivity.handler_ != null) { 
     201                Message m2 = Message.obtain(CallActivity.handler_, 
     202                    MSG_TYPE.CALL_MEDIA_STATE, 
     203                    null); 
     204                m2.sendToTarget(); 
     205            } 
     206 
     207        } else if (m.what == MSG_TYPE.BUDDY_STATE) { 
     208 
     209            MyBuddy buddy = (MyBuddy) m.obj; 
     210            int idx = account.buddyList.indexOf(buddy); 
     211 
     212            /* Update buddy status text, if buddy is valid and 
     213            * the buddy lists in account and UI are sync-ed. 
     214            */ 
     215            if (idx >= 0 && account.buddyList.size() == buddyList.size()) 
     216            { 
     217                buddyList.get(idx).put("status", buddy.getStatusText()); 
     218                buddyListAdapter.notifyDataSetChanged(); 
     219                // TODO: selection color/mark is gone after this, 
     220                //       dont know how to return it back. 
     221                //buddyListView.setSelection(buddyListSelectedIdx); 
     222                //buddyListView.performItemClick(buddyListView, 
     223                //                                   buddyListSelectedIdx, 
     224                //                                   buddyListView. 
     225                //                  getItemIdAtPosition(buddyListSelectedIdx)); 
     226 
     227                /* Return back Call activity */ 
     228                notifyCallState(currentCall); 
     229            } 
     230 
     231        } else if (m.what == MSG_TYPE.REG_STATE) { 
     232 
     233            String msg_str = (String) m.obj; 
     234            lastRegStatus = msg_str; 
     235 
     236        } else if (m.what == MSG_TYPE.INCOMING_CALL) { 
     237 
     238            /* Incoming call */ 
     239            final MyCall call = (MyCall) m.obj; 
     240            CallOpParam prm = new CallOpParam(); 
     241 
     242            /* Only one call at anytime */ 
     243            if (currentCall != null) { 
     244                /* 
     245                prm.setStatusCode(pjsip_status_code.PJSIP_SC_BUSY_HERE); 
     246                try { 
     247                call.hangup(prm); 
     248                } catch (Exception e) {} 
     249                */ 
     250                // TODO: set status code 
     251                call.delete(); 
     252                return true; 
     253            } 
     254 
     255            /* Answer with ringing */ 
     256            prm.setStatusCode(pjsip_status_code.PJSIP_SC_RINGING); 
     257            try { 
     258                call.answer(prm); 
     259            } catch (Exception e) {} 
     260 
     261            currentCall = call; 
     262            showCallActivity(); 
     263 
     264        } else { 
     265 
     266            /* Message not handled */ 
     267            return false; 
     268 
     269        } 
     270 
     271        return true; 
     272    } 
     273 
     274 
     275    private void dlgAccountSetting() 
     276    { 
     277        LayoutInflater li = LayoutInflater.from(this); 
     278        View view = li.inflate(R.layout.dlg_account_config, null); 
     279 
     280        if (lastRegStatus.length()!=0) { 
     281            TextView tvInfo = (TextView)view.findViewById(R.id.textViewInfo); 
     282            tvInfo.setText("Last status: " + lastRegStatus); 
     283        } 
     284 
     285        AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     286        adb.setView(view); 
     287        adb.setTitle("Account Settings"); 
     288 
     289        final EditText etId    = (EditText)view.findViewById(R.id.editTextId); 
     290        final EditText etReg   = (EditText)view.findViewById(R.id.editTextRegistrar); 
     291        final EditText etProxy = (EditText)view.findViewById(R.id.editTextProxy); 
     292        final EditText etUser  = (EditText)view.findViewById(R.id.editTextUsername); 
     293        final EditText etPass  = (EditText)view.findViewById(R.id.editTextPassword); 
     294 
     295        etId.   setText(accCfg.getIdUri()); 
     296        etReg.  setText(accCfg.getRegConfig().getRegistrarUri()); 
     297        StringVector proxies = accCfg.getSipConfig().getProxies(); 
     298        if (proxies.size() > 0) 
     299            etProxy.setText(proxies.get(0)); 
     300        else 
     301            etProxy.setText(""); 
     302        AuthCredInfoVector creds = accCfg.getSipConfig().getAuthCreds(); 
     303        if (creds.size() > 0) { 
     304            etUser. setText(creds.get(0).getUsername()); 
     305            etPass. setText(creds.get(0).getData()); 
     306        } else { 
     307            etUser. setText(""); 
     308            etPass. setText(""); 
     309        } 
     310 
     311        adb.setCancelable(false); 
     312        adb.setPositiveButton("OK", 
     313            new DialogInterface.OnClickListener() 
     314            { 
     315                public void onClick(DialogInterface dialog,int id) 
     316                { 
     317                    String acc_id        = etId.getText().toString(); 
     318                    String registrar = etReg.getText().toString(); 
     319                    String proxy         = etProxy.getText().toString(); 
     320                    String username  = etUser.getText().toString(); 
     321                    String password  = etPass.getText().toString(); 
     322 
     323                    accCfg.setIdUri(acc_id); 
     324                    accCfg.getRegConfig().setRegistrarUri(registrar); 
     325                    AuthCredInfoVector creds = accCfg.getSipConfig(). 
     326                                                            getAuthCreds(); 
     327                    creds.clear(); 
     328                    if (username.length() != 0) { 
     329                        creds.add(new AuthCredInfo("Digest", "*", username, 0, 
     330                                                   password)); 
     331                    } 
     332                    StringVector proxies = accCfg.getSipConfig().getProxies(); 
     333                    proxies.clear(); 
     334                    if (proxy.length() != 0) { 
     335                        proxies.add(proxy); 
     336                    } 
     337 
     338                    /* Enable ICE */ 
     339                    accCfg.getNatConfig().setIceEnabled(true); 
     340 
     341                    /* Finally */ 
     342                    lastRegStatus = ""; 
     343                    try { 
     344                        account.modify(accCfg); 
     345                    } catch (Exception e) {} 
     346                } 
     347            } 
     348        ); 
     349        adb.setNegativeButton("Cancel", 
     350            new DialogInterface.OnClickListener() 
     351            { 
     352                public void onClick(DialogInterface dialog,int id) 
     353                { 
     354                    dialog.cancel(); 
     355                } 
     356            } 
     357        ); 
     358 
     359        AlertDialog ad = adb.create(); 
     360        ad.show(); 
     361    } 
     362 
     363 
     364    public void makeCall(View view) 
     365    { 
     366        if (buddyListSelectedIdx == -1) 
     367            return; 
     368 
     369        /* Only one call at anytime */ 
     370        if (currentCall != null) { 
     371            return; 
     372        } 
     373 
     374        HashMap<String, String> item = (HashMap<String, String>) buddyListView. 
     375                                       getItemAtPosition(buddyListSelectedIdx); 
     376        String buddy_uri = item.get("uri"); 
     377 
     378        MyCall call = new MyCall(account, -1); 
     379        CallOpParam prm = new CallOpParam(true); 
     380 
     381        try { 
     382            call.makeCall(buddy_uri, prm); 
     383        } catch (Exception e) { 
     384            call.delete(); 
     385            return; 
     386        } 
     387 
     388        currentCall = call; 
     389        showCallActivity(); 
     390    } 
     391 
     392    private void dlgAddEditBuddy(BuddyConfig initial) 
     393    { 
     394        final BuddyConfig cfg = new BuddyConfig(); 
     395        final BuddyConfig old_cfg = initial; 
     396        final boolean is_add = initial == null; 
     397 
     398        LayoutInflater li = LayoutInflater.from(this); 
     399        View view = li.inflate(R.layout.dlg_add_buddy, null); 
     400 
     401        AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     402        adb.setView(view); 
     403 
     404        final EditText etUri  = (EditText)view.findViewById(R.id.editTextUri); 
     405        final CheckBox cbSubs = (CheckBox)view.findViewById(R.id.checkBoxSubscribe); 
     406 
     407        if (is_add) { 
     408            adb.setTitle("Add Buddy"); 
     409        } else { 
     410            adb.setTitle("Edit Buddy"); 
     411            etUri. setText(initial.getUri()); 
     412            cbSubs.setChecked(initial.getSubscribe()); 
     413        } 
     414 
     415        adb.setCancelable(false); 
     416        adb.setPositiveButton("OK", 
     417            new DialogInterface.OnClickListener() 
     418            { 
     419                public void onClick(DialogInterface dialog,int id) 
     420                { 
     421                    cfg.setUri(etUri.getText().toString()); 
     422                    cfg.setSubscribe(cbSubs.isChecked()); 
     423 
     424                    if (is_add) { 
     425                        account.addBuddy(cfg); 
     426                        buddyList.add(putData(cfg.getUri(), "")); 
     427                        buddyListAdapter.notifyDataSetChanged(); 
     428                        buddyListSelectedIdx = -1; 
     429                    } else { 
     430                        if (!old_cfg.getUri().equals(cfg.getUri())) { 
     431                            account.delBuddy(buddyListSelectedIdx); 
     432                            account.addBuddy(cfg); 
     433                            buddyList.remove(buddyListSelectedIdx); 
     434                            buddyList.add(putData(cfg.getUri(), "")); 
     435                            buddyListAdapter.notifyDataSetChanged(); 
     436                            buddyListSelectedIdx = -1; 
     437                        } else if (old_cfg.getSubscribe() !=  
     438                                   cfg.getSubscribe()) 
     439                        { 
     440                            MyBuddy bud = account.buddyList.get( 
     441                                                        buddyListSelectedIdx); 
     442                            try { 
     443                                bud.subscribePresence(cfg.getSubscribe()); 
     444                            } catch (Exception e) {} 
    92445                        } 
    93                          
    94                     app.init(this, getFilesDir().getAbsolutePath()); 
     446                    } 
    95447                } 
    96                  
    97             if (app.accList.size() == 0) { 
    98                 accCfg = new AccountConfig(); 
    99                 accCfg.setIdUri("sip:localhost"); 
    100                 accCfg.getNatConfig().setIceEnabled(true); 
    101                 account = app.addAcc(accCfg); 
    102             } else { 
    103                 account = app.accList.get(0); 
    104                 accCfg = account.cfg; 
    105             } 
    106              
    107                 buddyList = new ArrayList<Map<String, String>>(); 
    108                 for (int i = 0; i < account.buddyList.size(); i++) { 
    109                         buddyList.add(putData(account.buddyList.get(i).cfg.getUri(), 
    110                                                                   account.buddyList.get(i).getStatusText())); 
     448            } 
     449        ); 
     450        adb.setNegativeButton("Cancel", 
     451            new DialogInterface.OnClickListener() 
     452            { 
     453                public void onClick(DialogInterface dialog,int id) { 
     454                    dialog.cancel(); 
    111455                } 
    112  
    113                 String[] from = { "uri", "status" }; 
    114             int[] to = { android.R.id.text1, android.R.id.text2 }; 
    115             buddyListAdapter = new SimpleAdapter(this, buddyList, android.R.layout.simple_list_item_2, from, to); 
    116              
    117                 buddyListView = (ListView) findViewById(R.id.listViewBuddy);; 
    118             buddyListView.setAdapter(buddyListAdapter); 
    119             buddyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    120                 @Override 
    121                 public void onItemClick(AdapterView<?> parent, final View view, 
    122                                 int position, long id)  
    123                 { 
    124                         view.setSelected(true); 
    125                         buddyListSelectedIdx = position; 
    126                 } 
    127             }); 
    128              
    129         } 
    130  
    131         @Override 
    132         public boolean onCreateOptionsMenu(Menu menu) { 
    133                 // Inflate the menu; this adds items to the action bar if it is present. 
    134                 getMenuInflater().inflate(R.menu.main, menu); 
    135                 return true; 
    136         } 
    137  
    138         @Override 
    139         public boolean onOptionsItemSelected(MenuItem item) { 
    140                 switch (item.getItemId()) { 
    141                 case R.id.action_acc_config: 
    142                         dlgAccountSetting(); 
     456            } 
     457        ); 
     458 
     459        AlertDialog ad = adb.create(); 
     460        ad.show(); 
     461    } 
     462 
     463    public void addBuddy(View view) 
     464    { 
     465        dlgAddEditBuddy(null); 
     466    } 
     467 
     468    public void editBuddy(View view) 
     469    { 
     470        if (buddyListSelectedIdx == -1) 
     471            return; 
     472 
     473        BuddyConfig old_cfg = account.buddyList.get(buddyListSelectedIdx).cfg; 
     474        dlgAddEditBuddy(old_cfg); 
     475    } 
     476 
     477    public void delBuddy(View view) { 
     478        if (buddyListSelectedIdx == -1) 
     479            return; 
     480 
     481        final HashMap<String, String> item = (HashMap<String, String>) 
     482                        buddyListView.getItemAtPosition(buddyListSelectedIdx); 
     483        String buddy_uri = item.get("uri"); 
     484 
     485        DialogInterface.OnClickListener ocl = 
     486                                        new DialogInterface.OnClickListener() 
     487        { 
     488            @Override 
     489            public void onClick(DialogInterface dialog, int which) 
     490            { 
     491                switch (which) { 
     492                    case DialogInterface.BUTTON_POSITIVE: 
     493                        account.delBuddy(buddyListSelectedIdx); 
     494                        buddyList.remove(item); 
     495                        buddyListAdapter.notifyDataSetChanged(); 
     496                        buddyListSelectedIdx = -1; 
    143497                        break; 
    144  
    145                 case R.id.action_quit: 
    146                         Message m = Message.obtain(handler, 0); 
    147                         m.sendToTarget(); 
    148                         break; 
    149                          
    150                 default: 
     498                    case DialogInterface.BUTTON_NEGATIVE: 
    151499                        break; 
    152500                } 
    153  
    154                 return true; 
    155         }        
    156  
    157         @Override 
    158         public boolean handleMessage(Message m) { 
    159                  
    160                 if (m.what == 0) { 
    161                          
    162                         app.deinit(); 
    163                         finish(); 
    164                         Runtime.getRuntime().gc(); 
    165                         android.os.Process.killProcess(android.os.Process.myPid()); 
    166                          
    167                 } else if (m.what == MSG_TYPE.CALL_STATE) { 
    168                          
    169                         CallInfo ci = (CallInfo) m.obj; 
    170                          
    171                         /* Forward the message to CallActivity */ 
    172                         if (CallActivity.handler_ != null) { 
    173                                 Message m2 = Message.obtain(CallActivity.handler_, MSG_TYPE.CALL_STATE, ci); 
    174                                 m2.sendToTarget(); 
    175                         } 
    176                          
    177                 } else if (m.what == MSG_TYPE.CALL_MEDIA_STATE) { 
    178                          
    179                         /* Forward the message to CallActivity */ 
    180                         if (CallActivity.handler_ != null) { 
    181                                 Message m2 = Message.obtain(CallActivity.handler_, MSG_TYPE.CALL_MEDIA_STATE, null); 
    182                                 m2.sendToTarget(); 
    183                         } 
    184                          
    185                 } else if (m.what == MSG_TYPE.BUDDY_STATE) { 
    186                          
    187                         MyBuddy buddy = (MyBuddy) m.obj; 
    188                         int idx = account.buddyList.indexOf(buddy); 
    189  
    190                         /* Update buddy status text, if buddy is valid and 
    191                          * the buddy lists in account and UI are sync-ed. 
    192                          */ 
    193                         if (idx >= 0 && account.buddyList.size() == buddyList.size()) { 
    194                                 buddyList.get(idx).put("status", buddy.getStatusText()); 
    195                                 buddyListAdapter.notifyDataSetChanged(); 
    196                                 // TODO: selection color/mark is gone after this, 
    197                                 //       dont know how to return it back. 
    198                                 //buddyListView.setSelection(buddyListSelectedIdx); 
    199                                 //buddyListView.performItemClick(buddyListView, buddyListSelectedIdx, 
    200                                 //                                                         buddyListView.getItemIdAtPosition(buddyListSelectedIdx)); 
    201                                  
    202                                 /* Return back Call activity */ 
    203                                 notifyCallState(currentCall); 
    204                         } 
    205                          
    206                 } else if (m.what == MSG_TYPE.REG_STATE) { 
    207                          
    208                         String msg_str = (String) m.obj; 
    209                         lastRegStatus = msg_str; 
    210                          
    211                 } else if (m.what == MSG_TYPE.INCOMING_CALL) { 
    212                          
    213                         /* Incoming call */ 
    214                         final MyCall call = (MyCall) m.obj; 
    215                         CallOpParam prm = new CallOpParam(); 
    216                          
    217                         /* Only one call at anytime */ 
    218                         if (currentCall != null) { 
    219                                 /* 
    220                                 prm.setStatusCode(pjsip_status_code.PJSIP_SC_BUSY_HERE); 
    221                                 try { 
    222                                         call.hangup(prm); 
    223                                 } catch (Exception e) {} 
    224                                 */ 
    225                                 // TODO: set status code 
    226                                 call.delete(); 
    227                                 return true; 
    228                         } 
    229  
    230                         /* Answer with ringing */ 
    231                         prm.setStatusCode(pjsip_status_code.PJSIP_SC_RINGING); 
    232                         try { 
    233                                 call.answer(prm); 
    234                         } catch (Exception e) {} 
    235                          
    236                         currentCall = call; 
    237                         showCallActivity(); 
    238  
    239                 } else { 
    240                          
    241                         /* Message not handled */ 
    242                         return false; 
    243                          
    244                 } 
    245                          
    246                 return true; 
    247         } 
    248          
    249  
    250         private void dlgAccountSetting() { 
    251                  
    252                 LayoutInflater li = LayoutInflater.from(this); 
    253                 View view = li.inflate(R.layout.dlg_account_config, null); 
    254                  
    255                 if (lastRegStatus.length()!=0) { 
    256                         TextView tvInfo = (TextView)view.findViewById(R.id.textViewInfo); 
    257                         tvInfo.setText("Last status: " + lastRegStatus); 
    258                 } 
    259  
    260                 AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    261                 adb.setView(view); 
    262                 adb.setTitle("Account Settings"); 
    263  
    264                 final EditText etId    = (EditText)view.findViewById(R.id.editTextId); 
    265                 final EditText etReg   = (EditText)view.findViewById(R.id.editTextRegistrar); 
    266                 final EditText etProxy = (EditText)view.findViewById(R.id.editTextProxy); 
    267                 final EditText etUser  = (EditText)view.findViewById(R.id.editTextUsername); 
    268                 final EditText etPass  = (EditText)view.findViewById(R.id.editTextPassword); 
    269                  
    270                 etId.   setText(accCfg.getIdUri()); 
    271                 etReg.  setText(accCfg.getRegConfig().getRegistrarUri()); 
    272                 StringVector proxies = accCfg.getSipConfig().getProxies(); 
    273                 if (proxies.size() > 0) 
    274                         etProxy.setText(proxies.get(0)); 
    275                 else 
    276                         etProxy.setText(""); 
    277                 AuthCredInfoVector creds = accCfg.getSipConfig().getAuthCreds(); 
    278                 if (creds.size() > 0) { 
    279                         etUser. setText(creds.get(0).getUsername()); 
    280                         etPass. setText(creds.get(0).getData()); 
    281                 } else { 
    282                         etUser. setText(""); 
    283                         etPass. setText(""); 
    284                 } 
    285                  
    286                 adb.setCancelable(false); 
    287                 adb.setPositiveButton("OK", 
    288                           new DialogInterface.OnClickListener() { 
    289                             public void onClick(DialogInterface dialog,int id) { 
    290                                 String acc_id    = etId.getText().toString(); 
    291                                 String registrar = etReg.getText().toString(); 
    292                                 String proxy     = etProxy.getText().toString(); 
    293                                 String username  = etUser.getText().toString(); 
    294                                 String password  = etPass.getText().toString(); 
    295                                  
    296                                 accCfg.setIdUri(acc_id); 
    297                                 accCfg.getRegConfig().setRegistrarUri(registrar); 
    298                                         AuthCredInfoVector creds = accCfg.getSipConfig().getAuthCreds(); 
    299                                         creds.clear(); 
    300                                         if (username.length() != 0) { 
    301                                                 creds.add(new AuthCredInfo("Digest", "*", username, 0, password)); 
    302                                         } 
    303                                         StringVector proxies = accCfg.getSipConfig().getProxies(); 
    304                                         proxies.clear(); 
    305                                         if (proxy.length() != 0) { 
    306                                                 proxies.add(proxy); 
    307                                         } 
    308                                          
    309                                 /* Enable ICE */ 
    310                                 accCfg.getNatConfig().setIceEnabled(true); 
    311                                  
    312                                         /* Finally */ 
    313                                         lastRegStatus = ""; 
    314                                         try { 
    315                                                 account.modify(accCfg); 
    316                                         } catch (Exception e) {} 
    317                             } 
    318                           }); 
    319                 adb.setNegativeButton("Cancel", 
    320                           new DialogInterface.OnClickListener() { 
    321                             public void onClick(DialogInterface dialog,int id) { 
    322                                 dialog.cancel(); 
    323                             } 
    324                           }); 
    325  
    326                 AlertDialog ad = adb.create(); 
    327                 ad.show(); 
    328         } 
    329          
    330          
    331         public void makeCall(View view) { 
    332                 if (buddyListSelectedIdx == -1) 
    333                         return; 
    334                  
    335                 /* Only one call at anytime */ 
    336                 if (currentCall != null) { 
    337                         return; 
    338                 } 
    339                  
    340                 HashMap<String, String> item = (HashMap<String, String>) buddyListView.getItemAtPosition(buddyListSelectedIdx); 
    341                 String buddy_uri = item.get("uri"); 
    342                  
    343                 MyCall call = new MyCall(account, -1); 
    344                 CallOpParam prm = new CallOpParam(true); 
    345  
    346                 try { 
    347                         call.makeCall(buddy_uri, prm); 
    348                 } catch (Exception e) { 
    349                         call.delete(); 
    350                         return; 
    351                 } 
    352                  
    353                 currentCall = call; 
    354                 showCallActivity(); 
    355         } 
    356  
    357         private void dlgAddEditBuddy(BuddyConfig initial) { 
    358                 final BuddyConfig cfg = new BuddyConfig(); 
    359                 final BuddyConfig old_cfg = initial; 
    360                 final boolean is_add = initial == null; 
    361                  
    362                 LayoutInflater li = LayoutInflater.from(this); 
    363                 View view = li.inflate(R.layout.dlg_add_buddy, null); 
    364  
    365                 AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    366                 adb.setView(view); 
    367  
    368                 final EditText etUri    = (EditText)view.findViewById(R.id.editTextUri); 
    369                 final CheckBox cbSubs  = (CheckBox)view.findViewById(R.id.checkBoxSubscribe); 
    370  
    371                 if (is_add) { 
    372                         adb.setTitle("Add Buddy"); 
    373                 } else { 
    374                         adb.setTitle("Edit Buddy"); 
    375                         etUri. setText(initial.getUri()); 
    376                         cbSubs.setChecked(initial.getSubscribe()); 
    377                 } 
    378  
    379                 adb.setCancelable(false); 
    380                 adb.setPositiveButton("OK", 
    381                           new DialogInterface.OnClickListener() { 
    382                             public void onClick(DialogInterface dialog,int id) { 
    383                                 cfg.setUri(etUri.getText().toString()); 
    384                                 cfg.setSubscribe(cbSubs.isChecked()); 
    385                                  
    386                                 if (is_add) { 
    387                                         account.addBuddy(cfg); 
    388                                                 buddyList.add(putData(cfg.getUri(), "")); 
    389                                                 buddyListAdapter.notifyDataSetChanged(); 
    390                                                 buddyListSelectedIdx = -1; 
    391                                 } else { 
    392                                         if (!old_cfg.getUri().equals(cfg.getUri())) { 
    393                                                 account.delBuddy(buddyListSelectedIdx); 
    394                                                 account.addBuddy(cfg); 
    395                                                         buddyList.remove(buddyListSelectedIdx); 
    396                                                         buddyList.add(putData(cfg.getUri(), "")); 
    397                                                 buddyListAdapter.notifyDataSetChanged(); 
    398                                                 buddyListSelectedIdx = -1; 
    399                                         } else if (old_cfg.getSubscribe() != cfg.getSubscribe()) { 
    400                                                 MyBuddy bud = account.buddyList.get(buddyListSelectedIdx); 
    401                                                         try { 
    402                                                         bud.subscribePresence(cfg.getSubscribe()); 
    403                                                         } catch (Exception e) {} 
    404                                         } 
    405                                 } 
    406                             } 
    407                           }); 
    408                 adb.setNegativeButton("Cancel", 
    409                           new DialogInterface.OnClickListener() { 
    410                             public void onClick(DialogInterface dialog,int id) { 
    411                                 dialog.cancel(); 
    412                             } 
    413                           }); 
    414  
    415                 AlertDialog ad = adb.create(); 
    416                 ad.show(); 
    417         } 
    418          
    419         public void addBuddy(View view) { 
    420                 dlgAddEditBuddy(null); 
    421         } 
    422  
    423         public void editBuddy(View view) { 
    424                 if (buddyListSelectedIdx == -1) 
    425                         return; 
    426                  
    427                 BuddyConfig old_cfg = account.buddyList.get(buddyListSelectedIdx).cfg; 
    428                 dlgAddEditBuddy(old_cfg); 
    429         } 
    430          
    431         public void delBuddy(View view) { 
    432                 if (buddyListSelectedIdx == -1) 
    433                         return; 
    434                  
    435                 final HashMap<String, String> item = (HashMap<String, String>) buddyListView.getItemAtPosition(buddyListSelectedIdx); 
    436                 String buddy_uri = item.get("uri"); 
    437                  
    438                 DialogInterface.OnClickListener ocl = new DialogInterface.OnClickListener() { 
    439                     @Override 
    440                     public void onClick(DialogInterface dialog, int which) { 
    441                         switch (which) { 
    442                         case DialogInterface.BUTTON_POSITIVE: 
    443                                 account.delBuddy(buddyListSelectedIdx); 
    444                                 buddyList.remove(item); 
    445                                 buddyListAdapter.notifyDataSetChanged(); 
    446                                 buddyListSelectedIdx = -1; 
    447                             break; 
    448                         case DialogInterface.BUTTON_NEGATIVE: 
    449                             break; 
    450                         } 
    451                     } 
    452                 }; 
    453  
    454                 AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    455                 adb.setTitle(buddy_uri); 
    456                 adb.setMessage("\nDelete this buddy?\n"); 
    457                 adb.setPositiveButton("Yes", ocl); 
    458                 adb.setNegativeButton("No", ocl); 
    459                 adb.show(); 
    460         } 
    461          
    462          
    463         /* 
    464          * === MyAppObserver === 
    465          *  
    466          * As we cannot do UI from worker thread, the callbacks mostly just send 
    467          * a message to UI/main thread. 
    468          */ 
    469          
    470         public void notifyIncomingCall(MyCall call) { 
    471                 Message m = Message.obtain(handler, MSG_TYPE.INCOMING_CALL, call); 
    472                 m.sendToTarget(); 
    473         } 
    474  
    475         public void notifyRegState(pjsip_status_code code, String reason, int expiration) { 
    476                 String msg_str = ""; 
    477                 if (expiration == 0) 
    478                         msg_str += "Unregistration"; 
    479                 else 
    480                         msg_str += "Registration"; 
    481                  
    482                 if (code.swigValue()/100 == 2) 
    483                         msg_str += " successful"; 
    484                 else 
    485                         msg_str += " failed: " + reason; 
    486                  
    487                 Message m = Message.obtain(handler, MSG_TYPE.REG_STATE, msg_str); 
    488                 m.sendToTarget(); 
    489         } 
    490          
    491         public void notifyCallState(MyCall call) { 
    492                 if (currentCall == null || call.getId() != currentCall.getId()) 
    493                         return; 
    494                  
    495                 CallInfo ci; 
    496                 try { 
    497                         ci = call.getInfo(); 
    498                 } catch (Exception e) { 
    499                         ci = null; 
    500                 } 
    501                 Message m = Message.obtain(handler, MSG_TYPE.CALL_STATE, ci); 
    502                 m.sendToTarget(); 
    503                  
    504                 if (ci != null && 
    505                         ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
    506                 { 
    507                         currentCall = null; 
    508                 } 
    509         } 
    510          
    511         public void notifyCallMediaState(MyCall call) { 
    512                 Message m = Message.obtain(handler, MSG_TYPE.CALL_MEDIA_STATE, null); 
    513                 m.sendToTarget(); 
    514         } 
    515          
    516         public void notifyBuddyState(MyBuddy buddy) { 
    517                 Message m = Message.obtain(handler, MSG_TYPE.BUDDY_STATE, buddy); 
    518                 m.sendToTarget(); 
    519         } 
    520  
    521         /* === end of MyAppObserver ==== */ 
     501            } 
     502        }; 
     503 
     504        AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     505        adb.setTitle(buddy_uri); 
     506        adb.setMessage("\nDelete this buddy?\n"); 
     507        adb.setPositiveButton("Yes", ocl); 
     508        adb.setNegativeButton("No", ocl); 
     509        adb.show(); 
     510    } 
     511 
     512 
     513    /* 
     514    * === MyAppObserver === 
     515    *  
     516    * As we cannot do UI from worker thread, the callbacks mostly just send 
     517    * a message to UI/main thread. 
     518    */ 
     519 
     520    public void notifyIncomingCall(MyCall call) 
     521    { 
     522        Message m = Message.obtain(handler, MSG_TYPE.INCOMING_CALL, call); 
     523        m.sendToTarget(); 
     524    } 
     525 
     526    public void notifyRegState(pjsip_status_code code, String reason, 
     527                               int expiration) 
     528    { 
     529        String msg_str = ""; 
     530        if (expiration == 0) 
     531            msg_str += "Unregistration"; 
     532        else 
     533            msg_str += "Registration"; 
     534 
     535        if (code.swigValue()/100 == 2) 
     536            msg_str += " successful"; 
     537        else 
     538            msg_str += " failed: " + reason; 
     539 
     540        Message m = Message.obtain(handler, MSG_TYPE.REG_STATE, msg_str); 
     541        m.sendToTarget(); 
     542    } 
     543 
     544    public void notifyCallState(MyCall call) 
     545    { 
     546        if (currentCall == null || call.getId() != currentCall.getId()) 
     547            return; 
     548 
     549        CallInfo ci; 
     550        try { 
     551            ci = call.getInfo(); 
     552        } catch (Exception e) { 
     553            ci = null; 
     554        } 
     555        Message m = Message.obtain(handler, MSG_TYPE.CALL_STATE, ci); 
     556        m.sendToTarget(); 
     557 
     558        if (ci != null && 
     559            ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
     560        { 
     561            currentCall = null; 
     562        } 
     563    } 
     564 
     565    public void notifyCallMediaState(MyCall call) 
     566    { 
     567        Message m = Message.obtain(handler, MSG_TYPE.CALL_MEDIA_STATE, null); 
     568        m.sendToTarget(); 
     569    } 
     570 
     571    public void notifyBuddyState(MyBuddy buddy) 
     572    { 
     573        Message m = Message.obtain(handler, MSG_TYPE.BUDDY_STATE, buddy); 
     574        m.sendToTarget(); 
     575    } 
     576 
     577    /* === end of MyAppObserver ==== */ 
    522578 
    523579} 
Note: See TracChangeset for help on using the changeset viewer.