Code example for SipProfile

Methods: getProfileName, getUriString

0
        if (resultCode != RESULT_OK && resultCode != RESULT_FIRST_USER) return;
        new Thread() {
            public void run() { 
            try { 
                if (mProfile != null) {
                    Log.v(TAG, "Removed Profile:" + mProfile.getProfileName());
                    deleteProfile(mProfile);
                } 
 
                SipProfile profile = intent.getParcelableExtra(KEY_SIP_PROFILE);
                if (resultCode == RESULT_OK) {
                    Log.v(TAG, "New Profile Name:" + profile.getProfileName());
                    addProfile(profile);
                } 
                updateProfilesStatus(); 
            } catch (IOException e) {
                Log.v(TAG, "Can not handle the profile : " + e.getMessage());
            } 
        }}.start();
    } 
 
    private void registerForReceiveCallsCheckBox() { 
        mButtonSipReceiveCalls = (CheckBoxPreference) findPreference
                (BUTTON_SIP_RECEIVE_CALLS);
        mButtonSipReceiveCalls.setChecked(
                mSipSharedPreferences.isReceivingCallsEnabled());
        mButtonSipReceiveCalls.setOnPreferenceClickListener(
                new OnPreferenceClickListener() {
                    public boolean onPreferenceClick(Preference preference) {
                        final boolean enabled =
                                ((CheckBoxPreference) preference).isChecked();
                        new Thread(new Runnable() {
                                public void run() { 
                                    handleSipReceiveCallsOption(enabled);
                                } 
                        }).start();
                        return true; 
                    } 
                }); 
    } 
 
    private synchronized void handleSipReceiveCallsOption(boolean enabled) {
        mSipSharedPreferences.setReceivingCallsEnabled(enabled);
        List<SipProfile> sipProfileList = mProfileDb.retrieveSipProfileList();
        for (SipProfile p : sipProfileList) {
            String sipUri = p.getUriString();
            p = updateAutoRegistrationFlag(p, enabled);
            try { 
                if (enabled) {
                    mSipManager.open(p,
                            SipUtil.createIncomingCallPendingIntent(), null); 
                } else { 
                    mSipManager.close(sipUri);
                    if (mSipSharedPreferences.isPrimaryAccount(sipUri)) {
                        // re-open in order to make calls 
                        mSipManager.open(p);
                    } 
                } 
            } catch (Exception e) {
                Log.e(TAG, "register failed", e);
            } 
        } 
        updateProfilesStatus(); 
    } 
 
    private SipProfile updateAutoRegistrationFlag(
            SipProfile p, boolean enabled) {
        SipProfile newProfile = new SipProfile.Builder(p)
                .setAutoRegistration(enabled)
                .build();
        try { 
            mProfileDb.deleteProfile(p);
            mProfileDb.saveProfile(newProfile);
        } catch (Exception e) {
            Log.e(TAG, "updateAutoRegistrationFlag error", e);
        } 
        return newProfile;
    } 
 
    private void updateProfilesStatus() { 
        new Thread(new Runnable() {
            public void run() { 
                try { 
                    retrieveSipLists(); 
                } catch (Exception e) {
                    Log.e(TAG, "isRegistered", e);
                } 
            } 
        }).start();
    } 
 
    private String getProfileName(SipProfile profile) {
        String profileName = profile.getProfileName();
        if (TextUtils.isEmpty(profileName)) {
            profileName = profile.getUserName() + "@" + profile.getSipDomain();
        } 
        return profileName;
    } 
 
    private void retrieveSipLists() { 
        mSipPreferenceMap = new LinkedHashMap<String, SipPreference>();
        mSipProfileList = mProfileDb.retrieveSipProfileList();
        processActiveProfilesFromSipService(); 
        Collections.sort(mSipProfileList, new Comparator<SipProfile>() {
            public int compare(SipProfile p1, SipProfile p2) {
                return getProfileName(p1).compareTo(getProfileName(p2));
            } 
 
            public boolean equals(SipProfile p) {
                // not used 
                return false; 
            } 
        }); 
        mSipListContainer.removeAll();
        for (SipProfile p : mSipProfileList) {
            addPreferenceFor(p);
        } 
 
        if (!mSipSharedPreferences.isReceivingCallsEnabled()) return;
        for (SipProfile p : mSipProfileList) {
            if (mUid == p.getCallingUid()) {
                try { 
                    mSipManager.setRegistrationListener(
                            p.getUriString(), createRegistrationListener());
                } catch (SipException e) {
                    Log.e(TAG, "cannot set registration listener", e);
                } 
            } 
        } 
    } 
 
    private void processActiveProfilesFromSipService() { 
        SipProfile[] activeList = mSipManager.getListOfProfiles();
        for (SipProfile activeProfile : activeList) {
            SipProfile profile = getProfileFromList(activeProfile);
            if (profile == null) {
                mSipProfileList.add(activeProfile);
            } else { 
                profile.setCallingUid(activeProfile.getCallingUid());
            } 
        } 
    } 
 
    private SipProfile getProfileFromList(SipProfile activeProfile) {
        for (SipProfile p : mSipProfileList) {
            if (p.getUriString().equals(activeProfile.getUriString())) {
                return p;
            } 
        } 
        return null; 
    } 
 
    private void addPreferenceFor(SipProfile p) {
        String status;
        Log.v(TAG, "addPreferenceFor profile uri" + p.getUri());
        SipPreference pref = new SipPreference(this, p);
        mSipPreferenceMap.put(p.getUriString(), pref);
        mSipListContainer.addPreference(pref);
 
        pref.setOnPreferenceClickListener(
                new Preference.OnPreferenceClickListener() {
                    public boolean onPreferenceClick(Preference pref) {
                        handleProfileClick(((SipPreference) pref).mProfile);
                        return true; 
                    } 
                }); 
    } 
 
    private void handleProfileClick(final SipProfile profile) {
        int uid = profile.getCallingUid();
        if (uid == mUid || uid == 0) {
            startSipEditor(profile);
            return; 
        } 
        new AlertDialog.Builder(this)
                .setTitle(R.string.alert_dialog_close)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton(R.string.close_profile, 
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int w) {
                                deleteProfile(profile);
                                unregisterProfile(profile);
                            } 
                        }) 
                .setNegativeButton(android.R.string.cancel, null)
                .show(); 
    } 
 
    private void unregisterProfile(final SipProfile p) {
        // run it on background thread for better UI response 
        new Thread(new Runnable() {
            public void run() { 
                try { 
                    mSipManager.close(p.getUriString());
                } catch (Exception e) {
                    Log.e(TAG, "unregister failed, SipService died?", e);
                } 
            } 
        }, "unregisterProfile").start();
    } 
 
    void deleteProfile(SipProfile p) {
        mSipProfileList.remove(p);
        SipPreference pref = mSipPreferenceMap.remove(p.getUriString());
        mSipListContainer.removePreference(pref);
    } 
 
    private void addProfile(SipProfile p) throws IOException {
        try {