package io.agora.chatdemo.settings;

import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.preference.EditTextPreference;

import io.agora.chatdemo.R;
import io.agora.chatdemo.ui.BasePreferenceFragment;

public class NotificationSettingsPreference extends BasePreferenceFragment
        implements SharedPreferences.OnSharedPreferenceChangeListener{

    private String displayNameKey;
    private EditTextPreference mDisplayNamePreference;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences_notification, rootKey);

        displayNameKey = getActivity().getString(R.string.em_pref_key_notification_display_name);
        mDisplayNamePreference =
                (EditTextPreference) findPreference(displayNameKey);
    }

    /**
     * check display name preference content
     */
    private void checkDisplayName() {
        if (mDisplayNamePreference.getText() != null && mDisplayNamePreference.getText()
                .equals("")) {
            mDisplayNamePreference.setSummary("Input display name");
        } else {
            mDisplayNamePreference.setSummary(mDisplayNamePreference.getText());
        }
    }

    @Override public void onResume() {
        super.onResume();
        checkDisplayName();
        getPreferenceManager().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override public void onPause() {
        getPreferenceManager().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(displayNameKey)) {
            checkDisplayName();
        }
    }
}


@@ -97,16 +97,76 @@
 .Bundle;
+%0Aimport android.text.TextUtils;%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -227,70 +227,283 @@
 ora.
-chatdemo.R;%0Aimport io.agora.chatdemo.ui.BasePreferenceFragment
+CallBack;%0Aimport io.agora.chat.ChatClient;%0Aimport io.agora.chat.PushConfigs;%0Aimport io.agora.chatdemo.R;%0Aimport io.agora.chatdemo.ui.BasePreferenceFragment;%0Aimport io.agora.chatdemo.utils.ThreadPoolManager;%0Aimport io.agora.exceptions.ChatException;%0Aimport io.agora.util.EMLog
 ;%0A%0Ap
@@ -1112,16 +1112,962 @@
 meKey);%0A
+%0A        getDisplayNameFromServer();%0A    %7D%0A%0A    private void getDisplayNameFromServer() %7B%0A        ThreadPoolManager.getInstance().executeRunnable(()-%3E %7B%0A            PushConfigs configs = null;%0A            try %7B%0A                configs = ChatClient.getInstance().pushManager().getPushConfigsFromServer();%0A            %7D catch (ChatException e) %7B%0A                e.printStackTrace();%0A            %7D%0A            if(configs == null) %7B%0A                return;%0A            %7D%0A            String displayNickname = configs.getDisplayNickname();%0A            if(!TextUtils.isEmpty(displayNickname)) %7B%0A                if(getActivity() != null) %7B%0A                    getActivity().runOnUiThread(()-%3E %7B%0A                        if(getActivity() != null && !getActivity().isFinishing()) %7B%0A                            mDisplayNamePreference.setSummary(displayNickname);%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D%0A        %7D);%0A%0A
     %7D%0A%0A 
@@ -2176,55 +2176,99 @@
     
-if (mDisplayNamePreference.getText() != null &&
+String preName = (String) mDisplayNamePreference.getSummary();%0A        String displayName =
  mDi
@@ -2296,16 +2296,17 @@
 etText()
+;
 %0A       
@@ -2310,26 +2310,41 @@
     
-        .equals(%22%22
+if (TextUtils.isEmpty(displayName
 )) %7B
@@ -2480,52 +2480,1053 @@
 ary(
-mDisplayNamePreference.getText());%0A        %7D
+displayName);%0A            updatePushDisplayName(displayName, preName);%0A        %7D%0A    %7D%0A%0A    private void updatePushDisplayName(String displayName, String preName) %7B%0A        ChatClient.getInstance().pushManager().asyncUpdatePushNickname(displayName, new CallBack() %7B%0A            @Override%0A            public void onSuccess() %7B%0A                EMLog.d(%22update%22, %22update success%22);%0A            %7D%0A%0A            @Override%0A            public void onError(int code, String error) %7B%0A                if(getActivity() != null) %7B%0A                    getActivity().runOnUiThread(()-%3E %7B%0A                        if(getActivity() != null && !getActivity().isFinishing()) %7B%0A                            mDisplayNamePreference.setSummary(preName);%0A                            Toast.makeText(getActivity(), %22Failed to update display name%22, Toast.LENGTH_SHORT).show();%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onProgress(int progress, String status) %7B%0A%0A            %7D%0A        %7D);
 %0A   

Add update display name logic.