Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package it.fedeloper.smartcharging.Controller;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import it.fedeloper.smartcharging.Service.EndPoint;
import it.fedeloper.smartcharging.Service.HttpClient;
import it.fedeloper.smartcharging.Manager.PrefsManager;
import it.fedeloper.smartcharging.R;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class ManagerActivity extends FragmentActivity {
    Context c;
    Uri selectedAvatar;
    EditText nomeTxt;
    EditText telTxt;
    EditText emailTxt;
    EditText sitoTxt;
    EditText descTxt;
    Spinner spinTip;

    boolean nome=false;
    boolean tel=false;
    boolean email=false;
    boolean sito=false;
    boolean desc=false;
    boolean spin=false;
int tipologia;
    int i ;
    JSONObject o ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);
        c = this;
        Bundle b = new Bundle();
        b = getIntent().getExtras();

        i= b.getInt("i");








        nomeTxt=(EditText)findViewById(R.id.editText19);
        telTxt=(EditText)findViewById(R.id.editText20);
        emailTxt=(EditText)findViewById(R.id.editText21);
        sitoTxt=(EditText)findViewById(R.id.editText22);
        descTxt=(EditText)findViewById(R.id.editText23);
        spinTip=(Spinner)findViewById(R.id.spinner4);


        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.type_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinTip.setAdapter(adapter);

        try {
          o = SettingSelectionActivity.arr.getJSONObject(i);
            nomeTxt.setText( o.getString("nomelocale"));
            telTxt.setText( o.getString("telefono"));
            emailTxt.setText( o.getString("email"));
            sitoTxt.setText( o.getString("sito"));
            tipologia = o.getInt("tipologia");
            spinTip.setSelection(tipologia);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        nomeTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                LinearLayout l =  ((LinearLayout)nomeTxt.getParent());
                try {
                    if (s.length()==0 || nomeTxt.equals(o.getString("nomelocale"))) {
                        l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                        nome = false;
                    }
                    else
                    {
                        l.setBackgroundColor(Color.CYAN);
                        nome=true;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        telTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                LinearLayout l =  ((LinearLayout)telTxt.getParent());
                try {
                    if (s.length()==0 || telTxt.equals(o.getString("telefono"))) {
                        l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                        tel = false;
                    }
                    else
                    {
                        l.setBackgroundColor(Color.CYAN);
                        tel=true;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        emailTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                LinearLayout l =  ((LinearLayout)emailTxt.getParent());
                try {
                    if (s.length()==0 || emailTxt.equals(o.getString("email"))) {
                        l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                        email = false;
                    }
                    else
                    {
                        l.setBackgroundColor(Color.CYAN);
                        email=true;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        sitoTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                LinearLayout l =  ((LinearLayout)sitoTxt.getParent());
                try {
                    if (s.length()==0 || sitoTxt.equals(o.getString("sito"))) {
                        l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                        sito = false;
                    }
                    else
                    {
                        l.setBackgroundColor(Color.CYAN);
                        sito=true;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        descTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                LinearLayout l =  ((LinearLayout)descTxt.getParent());

                    if (s.length()==0) {
                        l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                        desc = false;
                    }
                    else
                    {
                        l.setBackgroundColor(Color.CYAN);
                        desc=true;
                    }


            }
        });
        spinTip.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // your code here
                LinearLayout l =  ((LinearLayout)spinTip.getParent());
                if (position != tipologia)
                {
                    l.setBackgroundColor(Color.CYAN);
                    spin=true;
                }

                else
                {
                    spin=false;
                    l.setBackgroundColor(Color.parseColor("#f0f0f0"));
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_manager, menu);
        return true;
    }

    Bitmap getOptimizedBitmap(Bitmap b, int maxms) {

        if (b.getWidth() > maxms)
            b = Bitmap.createScaledBitmap(b, b.getWidth() / (b.getWidth() / maxms), b.getHeight() / (b.getWidth() / maxms), false);


        if (b.getHeight() > maxms)
            b = Bitmap.createScaledBitmap(b, b.getWidth() / (b.getHeight() / maxms), b.getHeight() / (b.getHeight() / maxms), false);

        return b;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void clickpos(View v) {
        Intent in = new Intent(ManagerActivity.this, SetPositionLocaleActivity.class);
        startActivity(in);
    }

    public void clickimgs(View v) {
        Intent in = new Intent(ManagerActivity.this, ImageManagerActivity.class);
        startActivity(in);
    }

    public void clickdesc(View v) {
        new Thread(new Runnable() {
            public void run() {
                JSONObject obj = new JSONObject();
                try {

                    obj.put("id", PrefsManager.getId(c));
                    obj.put("descrizione", ((EditText) findViewById(R.id.editText)).getText().toString());
                    final JSONObject resp = HttpClient.SendHttpPostAsync(EndPoint.MAIN_END_POINT + "/set_descrizione.php", obj);


                    if (resp.getInt("return") == 0) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ManagerActivity.this);
                                dlgAlert.setMessage(R.string.descsuccess);
                                dlgAlert.setTitle("Info");
                                dlgAlert.setPositiveButton("OK", null);
                                dlgAlert.setCancelable(true);
                                dlgAlert.setPositiveButton("Ok",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                //dismiss the dialog
                                            }
                                        });
                                dlgAlert.create().show();
                            }
                        });
                    } else {
                        failed(getString(R.string.errordesc));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    public void clickavatar(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, 1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case 1000:
                if (resultCode == RESULT_OK) {
                    selectedAvatar = imageReturnedIntent.getData();
                    InputStream imageStream = null;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedAvatar);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);


                        ((ImageButton) findViewById(R.id.avatarImage)).setImageBitmap(getOptimizedBitmap(yourSelectedImage, 1500));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    new Thread(new Runnable() {
                        public void run() {


                            MultipartEntity mpEntity = new MultipartEntity();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Animation aAnimation = (Animation) AnimationUtils.loadAnimation(c, R.anim.zoom);


                                    ((ImageButton) findViewById(R.id.avatarImage)).startAnimation(aAnimation);
                                }
                            });

                            try {

                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                ((BitmapDrawable) ((ImageButton) findViewById(R.id.avatarImage)).getDrawable()).getBitmap().compress(Bitmap.CompressFormat.JPEG, 75, baos);
                                byte[] imageBytes = baos.toByteArray();

                                ContentBody bab = new ByteArrayBody(imageBytes, "file.jpeg");


                                ContentBody id_ = new StringBody(String.valueOf(PrefsManager.getId(c)));

                                ContentBody str_ = new StringBody(PrefsManager.getAvatar(c));

                                mpEntity.addPart("avatar_file", bab);
                                mpEntity.addPart("user_id", id_);
                                mpEntity.addPart("url", str_);

                                JSONObject resp = HttpClient.SendHttpPostDirect(EndPoint.MAIN_END_POINT +"/upload_avatar.php", mpEntity);
                                int id = resp.getInt("return");
                                switch (id) {
                                    case 0: {
                                        PrefsManager.setAvatar(c, resp.getString("avatar_path"));

                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {

                                                runOnUiThread(new Runnable() {

                                                    @Override
                                                    public void run() {
                                                        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ManagerActivity.this);
                                                        dlgAlert.setMessage(R.string.reavatarsuccess);
                                                        dlgAlert.setTitle("Info");
                                                        dlgAlert.setPositiveButton("OK", null);
                                                        dlgAlert.setCancelable(true);
                                                        dlgAlert.setPositiveButton("Ok",
                                                                new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                        //dismiss the dialog
                                                                    }
                                                                });
                                                        dlgAlert.create().show();
                                                    }
                                                });
                                                ((ImageButton) findViewById(R.id.avatarImage)).clearAnimation();
                                            }
                                        });
                                    }
                                    break;
                                    default: {
                                        failed(getString(R.string.avatar_error));
                                    }
                                }


                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();

                            } catch (JSONException e) {
                                e.printStackTrace();

                            }


                        }
                    }).start();
                }
                break;
        }
    }

    public void updateclick(View v)
    {
        updateLocal();
    }
    private void updateLocal() {
        new Thread(new Runnable() {
            public void run() {

                JSONObject obj = new JSONObject();
                try {
                  int mod=0;
                    if (nome){
                        mod++;
                        obj.put("nomelocale", nomeTxt.getText().toString());
                    }
                    if (tel){
                        mod++;
                        obj.put("telefono", telTxt.getText().toString());
                    }
                    if (sito){
                        mod++;
                        obj.put("sito", sitoTxt.getText().toString());
                    }
                    if (email){
                        mod++;
                        obj.put("email", emailTxt.getText().toString());
                    }
                    if (desc){
                        mod++;
                        obj.put("descrizione", descTxt.getText().toString());
                    }
                    if (spin){
                        mod++;
                        obj.put("tipologia",spinTip.getSelectedItemId());
                    }

                    obj.put("local_id",o.getInt("id"));

                    obj.put("token", PrefsManager.getToken(c));



                    JSONObject resp = HttpClient.SendHttpPostAsync(EndPoint.MAIN_END_POINT + "/edit_locale.php", obj);
                    int code = resp.getInt("return");

                    switch (code) {
                        case 0: {
                            Intent i = new Intent(ManagerActivity.this, SettingSelectionActivity.class);
                            startActivity(i);
                            finish();
                            Toast.makeText(getApplicationContext(), R.string.info_aggiornate,
                                    Toast.LENGTH_LONG).show();

                        }
                        break;
                        case 1: {
                           failed(getString(R.string.no_info_aggiornate));
                        }
                        break;
                        case -1: {
                           failed("Token error");
                        }
                        break;
                        default: {
                            failed("Server Error");
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void failed(final String error) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ManagerActivity.this);
                dlgAlert.setMessage(error);
                dlgAlert.setTitle(getString(R.string.errore_msg));
                dlgAlert.setPositiveButton("OK", null);
                dlgAlert.setCancelable(true);
                dlgAlert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //dismiss the dialog
                            }
                        });
                dlgAlert.create().show();
            }
        });
    }


}

Commits for Nextrek/Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Controller/ManagerActivity.java

Diff revisions: vs.
Revision Author Commited Message
525 Diff Diff FFontana picture FFontana Sat 22 Aug, 2015 11:08:25 +0000
524 Diff Diff FFontana picture FFontana Sat 22 Aug, 2015 10:47:42 +0000
521 Diff Diff MStefanelli picture MStefanelli Fri 21 Aug, 2015 12:38:59 +0000
518 Diff Diff FFontana picture FFontana Thu 20 Aug, 2015 14:36:46 +0000
505 FFontana picture FFontana Wed 19 Aug, 2015 08:54:46 +0000