Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.87 KB | None | 0 0
  1. public class UpdateProfileActivity extends AppCompatActivity {
  2.  
  3. Toolbar toolbar;
  4. ImageView imgProfile;
  5. Button btnSaveProfile,btnMyadd;
  6. EditText edtName,edtEmail,edtNumber,edtWebsite;
  7. TextView txtname,txtEmail,txtNumber,txtWebsite;
  8. ProgressDialog progressBar;
  9. String name,email,number,website;
  10. private String selectedImagePath = "";
  11. final private int PICK_IMAGE = 1;
  12. final private int CAPTURE_IMAGE = 2;
  13. private String imgPath;
  14. File file;
  15. boolean isphotoselected = false;
  16. private SharedPreferences pref;
  17. Typeface typeface;
  18.  
  19. String advertId,advertUserName,advertPassword,advertEmail,advertPhone,advertSite,advertImage;
  20. boolean AddFlag= false;
  21.  
  22. MarshMallowPermission marshMallowPermission;
  23. @Override
  24. protected void onCreate(@Nullable Bundle savedInstanceState) {
  25. // TODO Auto-generated method stub
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_updateprofile);
  28. toolbar = (Toolbar) findViewById(R.id.toolbar);
  29. typeface = Typeface.createFromAsset(getAssets(), "fonts/GandhiSerif-Bold.otf");
  30.  
  31. if (toolbar != null) {
  32. toolbar.setTitle("Profile Update");
  33. setSupportActionBar(toolbar);
  34. getSupportActionBar().setHomeButtonEnabled(true);
  35. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  36. }
  37.  
  38. getActionBarTextView();
  39. marshMallowPermission = new MarshMallowPermission(this);
  40. if (!marshMallowPermission.checkPermissionForExternalStorage()) {
  41. marshMallowPermission.requestPermissionForExternalStorage();
  42. }
  43. if (!marshMallowPermission.checkPermissionForCamera()) {
  44. marshMallowPermission.requestPermissionForCamera();
  45. }
  46. if (!marshMallowPermission.checkPermissionForExternalStorage()) {
  47. marshMallowPermission.requestPermissionForExternalStorage();
  48. }
  49. imgProfile = (ImageView)findViewById(R.id.imgProfile);
  50. btnMyadd = (Button)findViewById(R.id.btnMyadd);
  51. btnSaveProfile=(Button)findViewById(R.id.btnSaveprofile);
  52. edtEmail = (EditText)findViewById(R.id.edtEmailid);
  53. edtName = (EditText)findViewById(R.id.edtName);
  54. edtNumber = (EditText)findViewById(R.id.edtphone);
  55. edtWebsite= (EditText)findViewById(R.id.edtwebsite);
  56. txtEmail = (TextView)findViewById(R.id.txtEmail);
  57. txtname = (TextView)findViewById(R.id.txtName);
  58. txtNumber = (TextView)findViewById(R.id.txtPhone);
  59. txtWebsite= (TextView)findViewById(R.id.txtWeb);
  60.  
  61. btnMyadd.setTypeface(typeface);
  62. btnSaveProfile.setTypeface(typeface);
  63. edtEmail.setTypeface(typeface);
  64. edtName.setTypeface(typeface);
  65. edtNumber.setTypeface(typeface);
  66. edtWebsite.setTypeface(typeface);
  67. txtEmail.setTypeface(typeface);
  68. txtname.setTypeface(typeface);
  69. txtNumber.setTypeface(typeface);
  70. txtWebsite.setTypeface(typeface);
  71.  
  72.  
  73. pref = getSharedPreferences("loginpreference", MODE_PRIVATE);
  74.  
  75. imgProfile.setOnClickListener(new View.OnClickListener()
  76. {
  77.  
  78. @Override
  79. public void onClick(View arg0) {
  80. // TODO Auto-generated method stub
  81. startDialog();
  82. }
  83. });
  84.  
  85. btnSaveProfile.setOnClickListener(new View.OnClickListener()
  86. {
  87. @Override
  88. public void onClick(View arg0) {
  89. // TODO Auto-generated method stub
  90. name = edtName.getText().toString();
  91. number = edtNumber.getText().toString();
  92. email = edtEmail.getText().toString();
  93. website= edtWebsite.getText().toString();
  94.  
  95. boolean flag = emailValidator(email);
  96.  
  97. if(name.equalsIgnoreCase("")){
  98. edtName.setError("Please Enter Name");
  99. }else if(number.equalsIgnoreCase("")){
  100. edtNumber.setError("Please Enter Contact Number");
  101. }else if(flag == false){
  102. edtEmail.setError("Please Enter Email");
  103. }else if(website.equalsIgnoreCase("")){
  104. edtWebsite.setError("Please Enter Website");
  105. }else if(selectedImagePath.equalsIgnoreCase("") || selectedImagePath==null){
  106. Toast.makeText(getApplicationContext(), "Please Select Profile Picture", Toast.LENGTH_SHORT).show();
  107. }else {
  108. new UpdateProfileTask().execute();
  109. }
  110.  
  111. }
  112. });
  113.  
  114. btnMyadd.setOnClickListener(new View.OnClickListener()
  115. {
  116.  
  117. @Override
  118. public void onClick(View arg0) {
  119. // TODO Auto-generated method stub
  120.  
  121. if(!AddFlag){
  122. Toast.makeText(UpdateProfileActivity.this, "Please Update Your Profile", Toast.LENGTH_SHORT).show();
  123. }else{
  124. AddFlag = true;
  125. Intent intent =new Intent(UpdateProfileActivity.this, MyAdsActivity.class);
  126. intent.putExtra("advertId", advertId);
  127. startActivity(intent);
  128. }
  129.  
  130. }
  131. });
  132. }
  133. @Override
  134. public boolean onOptionsItemSelected(MenuItem item) {
  135. // Handle action bar item clicks here. The action bar will
  136. // automatically handle clicks on the Home/Up button, so long
  137. // as you specify a parent activity in AndroidManifest.xml.
  138. int id = item.getItemId();
  139.  
  140. if(id==android.R.id.home)
  141. {
  142. onBackPressed();
  143. }
  144. return super.onOptionsItemSelected(item);
  145. }
  146.  
  147. @Override
  148. public void onBackPressed() {
  149. // TODO Auto-generated method stub
  150. super.onBackPressed();
  151.  
  152. }
  153. public boolean emailValidator(String s)
  154. {
  155. return Pattern.compile("^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$").matcher(s).matches();
  156. }
  157.  
  158. private TextView getActionBarTextView() {
  159. TextView titleTextView = null;
  160.  
  161. try {
  162. Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
  163. f.setAccessible(true);
  164. titleTextView = (TextView)f.get(toolbar);
  165. titleTextView.setTypeface(typeface);
  166.  
  167. } catch (NoSuchFieldException e) {
  168. } catch (IllegalAccessException e) {
  169. }
  170. return titleTextView;
  171. }
  172.  
  173.  
  174. class UpdateProfileTask extends AsyncTask<Void, Void, Void>
  175. {
  176. String jsonStr = null;
  177. CustomProgressDialog cd= new CustomProgressDialog();
  178.  
  179. @Override
  180. protected void onPreExecute() {
  181. super.onPreExecute();
  182. cd.showdialog(UpdateProfileActivity.this, "Loading...");
  183.  
  184. }
  185. @Override
  186. protected Void doInBackground(Void... arg0) {
  187. jsonStr = uploadFile();
  188. return null;
  189. }
  190.  
  191. @Override
  192. protected void onPostExecute(Void result) {
  193. super.onPostExecute(result);
  194. cd.dismissdialog();
  195. if (jsonStr != null) {
  196. try {
  197. JSONObject jsonObj = new JSONObject(jsonStr);
  198. String responce = jsonObj.getString(Constants.LOGIN_TAG);
  199. String msg = jsonObj.getString(Constants.REG_TAG);
  200.  
  201. if(responce.equalsIgnoreCase("success")){
  202. JSONArray jArray = jsonObj.getJSONArray("data");
  203.  
  204. if(jArray.length()>0){
  205.  
  206. JSONObject j1 = jArray.getJSONObject(0);
  207. advertId = j1.getString("advertId");
  208. advertUserName = j1.getString("advertUserName");
  209. advertPassword = j1.getString("advertPassword");
  210. advertEmail = j1.getString("advertEmail");
  211. advertPhone = j1.getString("advertPhone");
  212. advertSite = j1.getString("advertSite");
  213. advertImage = j1.getString("advertImage");
  214. AddFlag = true;
  215. }
  216. Toast.makeText(UpdateProfileActivity.this, msg, Toast.LENGTH_SHORT).show();
  217. }else{
  218. Toast.makeText(UpdateProfileActivity.this, msg, Toast.LENGTH_SHORT).show();
  219. }
  220.  
  221. edtEmail.setText("");
  222. edtName.setText("");
  223. edtNumber.setText("");
  224. edtWebsite.setText("");
  225.  
  226. } catch (JSONException e) {
  227. e.printStackTrace();
  228. }
  229. } else {
  230. Log.e("ServiceHandler", "Couldn't get any data from the url");
  231. }
  232.  
  233. }
  234. }
  235.  
  236. @SuppressWarnings("deprecation")
  237. private String uploadFile() {
  238. String responseString = null;
  239. HttpClient httpclient = new DefaultHttpClient();
  240. HttpPost httppost = new HttpPost(Constants.WEBURL);
  241.  
  242. try {
  243. AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new AndroidMultiPartEntity.ProgressListener() {
  244. @Override
  245. public void transferred(long num) {
  246.  
  247. }
  248. });
  249. if(isphotoselected){
  250. file = new File(selectedImagePath);
  251. //entity.addPart("userfile",selectedImagePath.toString();
  252. entity.addPart("avtar", new FileBody(file));
  253. //entity.addPart("filesArray[]",new FileBody(file));
  254. isphotoselected = false;
  255. }
  256.  
  257. String advartid = pref.getString("advertId", "");
  258. entity.addPart("action", new StringBody("updateAccount"));
  259. entity.addPart("advertId", new StringBody(advartid));
  260. entity.addPart("username", new StringBody(name));
  261. entity.addPart("email", new StringBody(email));
  262. entity.addPart("phone", new StringBody(number));
  263. entity.addPart("site", new StringBody(website));
  264.  
  265. httppost.setEntity(entity);
  266.  
  267. // Making server call
  268. HttpResponse response = httpclient.execute(httppost);
  269. HttpEntity r_entity = response.getEntity();
  270.  
  271. int statusCode = response.getStatusLine().getStatusCode();
  272. if (statusCode == 200) {
  273. // Server response
  274. responseString = EntityUtils.toString(r_entity);
  275. } else {
  276. responseString = "Error occurred! Http Status Code: "+ statusCode;
  277. }
  278.  
  279. } catch (ClientProtocolException e) {
  280. responseString = e.toString();
  281. } catch (IOException e) {
  282. responseString = e.toString();
  283. }
  284.  
  285. return responseString;
  286.  
  287. }
  288. @Override
  289. protected void onActivityResult(int requestCode, int resultCode,
  290. Intent data) {
  291. if (resultCode != Activity.RESULT_CANCELED) {
  292. if (requestCode == PICK_IMAGE) {
  293. isphotoselected = true;
  294. selectedImagePath = getAbsolutePath(data.getData());
  295. imgProfile.setImageBitmap(decodeFile(selectedImagePath));
  296. } else if (requestCode == CAPTURE_IMAGE) {
  297. isphotoselected = true;
  298. selectedImagePath = getImagePath();
  299. imgProfile.setImageBitmap(decodeFile(selectedImagePath));
  300. } else {
  301. super.onActivityResult(requestCode, resultCode,
  302. data);
  303. }
  304. }
  305. }
  306.  
  307. private void startDialog()
  308. {
  309. android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
  310. builder.setTitle("Upload Pictures Option");
  311. builder.setMessage("How do you want to set your picture?");
  312. builder.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
  313.  
  314. public void onClick(DialogInterface dialoginterface, int i)
  315. {
  316. Intent intent = new Intent();
  317. intent.setType("image/*");
  318. intent.setAction(Intent.ACTION_GET_CONTENT);
  319. startActivityForResult(Intent.createChooser(intent, ""),PICK_IMAGE);
  320.  
  321. }
  322.  
  323. });
  324. builder.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
  325.  
  326. public void onClick(DialogInterface dialoginterface, int i)
  327. {
  328. final Intent intent = new Intent(
  329. MediaStore.ACTION_IMAGE_CAPTURE);
  330. intent.putExtra(MediaStore.EXTRA_OUTPUT,
  331. setImageUri());
  332. startActivityForResult(intent, CAPTURE_IMAGE);
  333. }
  334. });
  335. builder.show();
  336. }
  337.  
  338. public Uri setImageUri() {
  339. // Store image in dcim
  340. File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
  341. Uri imgUri = Uri.fromFile(file);
  342. this.imgPath = file.getAbsolutePath();
  343. return imgUri;
  344. }
  345.  
  346. public String getImagePath() {
  347. return imgPath;
  348. }
  349.  
  350. public Bitmap decodeFile(String path) {
  351. try {
  352. // Decode image size
  353. BitmapFactory.Options o = new BitmapFactory.Options();
  354. o.inJustDecodeBounds = true;
  355. BitmapFactory.decodeFile(path, o);
  356. // The new size we want to scale to
  357. final int REQUIRED_SIZE = 70;
  358.  
  359. // Find the correct scale value. It should be the power of
  360. // 2.
  361. int scale = 1;
  362. while (o.outWidth / scale / 2 >= REQUIRED_SIZE
  363. && o.outHeight / scale / 2 >= REQUIRED_SIZE)
  364. scale *= 2;
  365.  
  366. // Decode with inSampleSize
  367. BitmapFactory.Options o2 = new BitmapFactory.Options();
  368. o2.inSampleSize = scale;
  369. return BitmapFactory.decodeFile(path, o2);
  370. } catch (Throwable e) {
  371. e.printStackTrace();
  372. }
  373. return null;
  374. }
  375. public String getAbsolutePath(Uri uri) {
  376. String[] projection = { MediaColumns.DATA };
  377. Cursor cursor = managedQuery(uri, projection, null, null, null);
  378. if (cursor != null) {
  379. int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst();
  380. return cursor.getString(column_index);
  381. } else
  382. return null;
  383. }
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement