Ignore:
Timestamp:
Sep 15, 2017 5:32:08 AM (7 years ago)
Author:
riza
Message:

Re #2041: Implement API to handle IP address change.

File:
1 edited

Legend:

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

    r5502 r5649  
    1919package org.pjsip.pjsua2.app; 
    2020 
     21import android.content.IntentFilter; 
    2122import android.os.Bundle; 
    2223import android.os.Handler; 
     
    2728import android.content.Intent; 
    2829import android.content.pm.ApplicationInfo; 
     30import android.content.BroadcastReceiver; 
     31import android.content.Context; 
    2932import android.view.LayoutInflater; 
    3033import android.view.Menu; 
     
    3740import android.widget.SimpleAdapter; 
    3841import android.widget.TextView; 
     42import android.net.ConnectivityManager; 
     43import android.net.NetworkInfo; 
    3944 
    4045import java.util.ArrayList; 
     
    5156    public static MyAccount account = null; 
    5257    public static AccountConfig accCfg = null; 
     58    public static MyBroadcastReceiver receiver = null; 
     59    public static IntentFilter intentFilter = null; 
    5360 
    5461    private ListView buddyListView; 
     
    6673        public final static int BUDDY_STATE = 4; 
    6774        public final static int CALL_MEDIA_STATE = 5; 
     75        public final static int CHANGE_NETWORK = 6; 
     76    } 
     77 
     78    private class MyBroadcastReceiver extends BroadcastReceiver { 
     79        private String conn_name = ""; 
     80 
     81        @Override 
     82        public void onReceive(Context context, Intent intent) { 
     83            if (isNetworkChange(context)) 
     84                notifyChangeNetwork(); 
     85        } 
     86 
     87        private boolean isNetworkChange(Context context) { 
     88            boolean network_changed = false; 
     89            ConnectivityManager connectivity_mgr = 
     90                ((ConnectivityManager)context.getSystemService( 
     91                                                 Context.CONNECTIVITY_SERVICE)); 
     92 
     93            NetworkInfo net_info = connectivity_mgr.getActiveNetworkInfo(); 
     94            if(net_info != null && net_info.isConnectedOrConnecting() && 
     95               !conn_name.equalsIgnoreCase("")) 
     96            { 
     97                String new_con = net_info.getExtraInfo(); 
     98                if (new_con != null && !new_con.equalsIgnoreCase(conn_name)) 
     99                    network_changed = true; 
     100 
     101                conn_name = (new_con == null)?"":new_con; 
     102            } else { 
     103                if (conn_name.equalsIgnoreCase("")) 
     104                    conn_name = net_info.getExtraInfo(); 
     105            } 
     106            return network_changed; 
     107        } 
    68108    } 
    69109 
     
    144184            } 
    145185        ); 
    146  
     186        if (receiver == null) { 
     187            receiver = new MyBroadcastReceiver(); 
     188            intentFilter = new IntentFilter( 
     189                                       ConnectivityManager.CONNECTIVITY_ACTION); 
     190            registerReceiver(receiver, intentFilter); 
     191        } 
    147192    } 
    148193 
     
    264309            showCallActivity(); 
    265310 
     311        } else if (m.what == MSG_TYPE.CHANGE_NETWORK) { 
     312            app.handleNetworkChange(); 
    266313        } else { 
    267314 
     
    577624    } 
    578625 
     626    public void notifyChangeNetwork() 
     627    { 
     628        Message m = Message.obtain(handler, MSG_TYPE.CHANGE_NETWORK, null); 
     629        m.sendToTarget(); 
     630    } 
     631 
    579632    /* === end of MyAppObserver ==== */ 
    580633 
Note: See TracChangeset for help on using the changeset viewer.