private void saveName() { TLRPC.User currentUser = UserConfig.getCurrentUser(); if (currentUser == null || lastNameField.getText() == null || firstNameField.getText() == null) { return; } String newFirst = firstNameField.getText().toString(); String newLast = lastNameField.getText().toString(); if (currentUser.first_name != null && currentUser.first_name.equals(newFirst) && currentUser.last_name != null && currentUser.last_name.equals(newLast)) { return; } TLRPC.TL_account_updateProfile req = new TLRPC.TL_account_updateProfile(); currentUser.first_name = req.first_name = newFirst; currentUser.last_name = req.last_name = newLast; TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId()); if (user != null) { user.first_name = req.first_name; user.last_name = req.last_name; } UserConfig.saveConfig(true); NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged); NotificationCenter.getInstance() .postNotificationName( NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_NAME); ConnectionsManager.getInstance() .performRpc( req, new RPCRequest.RPCRequestDelegate() { @Override public void run(TLObject response, TLRPC.TL_error error) {} }); }
private void searchBingImages(String query, int offset, int count) { if (searching) { searching = false; requestQueue.cancelAll("search"); } try { searching = true; String url; if (nextSearchBingString != null) { url = nextSearchBingString; } else { boolean adult; String phone = UserConfig.getCurrentUser().phone; adult = phone.startsWith("44") || phone.startsWith("49") || phone.startsWith("43") || phone.startsWith("31") || phone.startsWith("1"); url = String.format( Locale.US, "https://api.datamarket.azure.com/Bing/Search/v1/Image?Query='%s'&$skip=%d&$top=%d&$format=json%s", URLEncoder.encode(query, "UTF-8"), offset, count, adult ? "" : "&Adult='Off'"); } JsonObjectRequest jsonObjReq = new JsonObjectRequest( Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { nextSearchBingString = null; try { JSONObject d = response.getJSONObject("d"); JSONArray result = d.getJSONArray("results"); try { nextSearchBingString = d.getString("__next"); } catch (Exception e) { nextSearchBingString = null; FileLog.e("tmessages", e); } for (int a = 0; a < result.length(); a++) { try { JSONObject object = result.getJSONObject(a); String id = Utilities.MD5(object.getString("MediaUrl")); if (searchResultKeys.containsKey(id)) { continue; } MediaController.SearchImage bingImage = new MediaController.SearchImage(); bingImage.id = id; bingImage.width = object.getInt("Width"); bingImage.height = object.getInt("Height"); bingImage.size = object.getInt("FileSize"); bingImage.imageUrl = object.getString("MediaUrl"); JSONObject thumbnail = object.getJSONObject("Thumbnail"); bingImage.thumbUrl = thumbnail.getString("MediaUrl"); searchResult.add(bingImage); searchResultKeys.put(id, bingImage); } catch (Exception e) { FileLog.e("tmessages", e); } } } catch (Exception e) { FileLog.e("tmessages", e); } searching = false; if (nextSearchBingString != null && !nextSearchBingString.contains("json")) { nextSearchBingString += "&$format=json"; } updateSearchInterface(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { FileLog.e("tmessages", "Error: " + error.getMessage()); nextSearchBingString = null; searching = false; updateSearchInterface(); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); String auth = "Basic " + Base64.encodeToString( (BuildVars.BING_SEARCH_KEY + ":" + BuildVars.BING_SEARCH_KEY).getBytes(), Base64.NO_WRAP); headers.put("Authorization", auth); return headers; } }; jsonObjReq.setTag("search"); requestQueue.add(jsonObjReq); } catch (Exception e) { FileLog.e("tmessages", e); nextSearchBingString = null; searching = false; updateSearchInterface(); } }
@Override public View createView(Context context, LayoutInflater inflater) { actionBar.setBackButtonImage(R.drawable.ic_ab_back); actionBar.setAllowOverlayTitle(true); actionBar.setTitle(LocaleController.getString("EditName", R.string.EditName)); actionBar.setActionBarMenuOnItemClick( new ActionBar.ActionBarMenuOnItemClick() { @Override public void onItemClick(int id) { if (id == -1) { finishFragment(); } else if (id == done_button) { if (firstNameField.getText().length() != 0) { saveName(); finishFragment(); } } } }); ActionBarMenu menu = actionBar.createMenu(); doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56)); TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId()); if (user == null) { user = UserConfig.getCurrentUser(); } fragmentView = new LinearLayout(context); fragmentView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ((LinearLayout) fragmentView).setOrientation(LinearLayout.VERTICAL); fragmentView.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); firstNameField = new EditText(context); firstNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); firstNameField.setHintTextColor(0xff979797); firstNameField.setTextColor(0xff212121); firstNameField.setMaxLines(1); firstNameField.setLines(1); firstNameField.setSingleLine(true); firstNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT); firstNameField.setInputType( InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); firstNameField.setImeOptions(EditorInfo.IME_ACTION_NEXT); firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName)); AndroidUtilities.clearCursorDrawable(firstNameField); ((LinearLayout) fragmentView).addView(firstNameField); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) firstNameField.getLayoutParams(); layoutParams.topMargin = AndroidUtilities.dp(24); layoutParams.height = AndroidUtilities.dp(36); layoutParams.leftMargin = AndroidUtilities.dp(24); layoutParams.rightMargin = AndroidUtilities.dp(24); layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT; firstNameField.setLayoutParams(layoutParams); firstNameField.setOnEditorActionListener( new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { if (i == EditorInfo.IME_ACTION_NEXT) { lastNameField.requestFocus(); lastNameField.setSelection(lastNameField.length()); return true; } return false; } }); lastNameField = new EditText(context); lastNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); lastNameField.setHintTextColor(0xff979797); lastNameField.setTextColor(0xff212121); lastNameField.setMaxLines(1); lastNameField.setLines(1); lastNameField.setSingleLine(true); lastNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT); lastNameField.setInputType( InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); lastNameField.setImeOptions(EditorInfo.IME_ACTION_DONE); lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName)); AndroidUtilities.clearCursorDrawable(lastNameField); ((LinearLayout) fragmentView).addView(lastNameField); layoutParams = (LinearLayout.LayoutParams) lastNameField.getLayoutParams(); layoutParams.topMargin = AndroidUtilities.dp(16); layoutParams.height = AndroidUtilities.dp(36); layoutParams.leftMargin = AndroidUtilities.dp(24); layoutParams.rightMargin = AndroidUtilities.dp(24); layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT; lastNameField.setLayoutParams(layoutParams); lastNameField.setOnEditorActionListener( new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { if (i == EditorInfo.IME_ACTION_DONE) { doneButton.performClick(); return true; } return false; } }); if (user != null) { firstNameField.setText(user.first_name); firstNameField.setSelection(firstNameField.length()); lastNameField.setText(user.last_name); } return fragmentView; }