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
package it.fedeloper.smartcharging.Manager;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.media.ExifInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Debug;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import it.fedeloper.smartcharging.R;

/**
 * Created by federico on 11/08/2015.
 */
public class Utils {


    public static String getTipologiaFromId(Context c,int id)
    {
        final String[] tipi = c.getResources().getStringArray(R.array.type_array);
        return tipi[id-1];
    }


    public static Uri getImageUri(Context inContext, Bitmap inImage) {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Images", null);
        Uri u=Uri.parse(path);
        String s=getRealPathFromURI(inContext,u);
        File f=new File(s);

        File file= new File(f.getParent(),".nomedia");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return u;
    }

    public static String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }


    public static int dpToPx(int dp, Context c) {
        DisplayMetrics displayMetrics = c.getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

    public static boolean isValidEmailAddress(String email) {
        String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
        java.util.regex.Matcher m = p.matcher(email);
        return m.matches();
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();

            return decodeBitmapFromStreamFixed(input, 1000, 1000);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Bitmap decodeBitmapFromStreamFixed(InputStream inputStream, int reqWidth, int reqHeight) {
        return decodeBitmapFromStreamFixed(inputStream, reqWidth, reqHeight, null);
    }

    public static Bitmap decodeBitmapFromStreamFixed(InputStream inputStream, int reqWidth, int reqHeight, String path){
        byte[] byteArr = new byte[0];
        byte[] buffer = new byte[1024];
        int len;
        int count = 0;

        try {
            while ((len = inputStream.read(buffer)) > -1) {
                if (len != 0) {
                    if (count + len > byteArr.length) {
                        byte[] newbuf = new byte[(count + len) * 2];
                        System.arraycopy(byteArr, 0, newbuf, 0, count);
                        byteArr = newbuf;
                    }

                    System.arraycopy(buffer, 0, byteArr, count, len);
                    count += len;
                }
            }

            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(byteArr, 0, count, options);

            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            options.inPurgeable = true;
            options.inInputShareable = true;
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            Bitmap finalBitmap = BitmapFactory.decodeByteArray(byteArr, 0, count, options);

            if(path==null)
                return finalBitmap;

            return rotateBitmap(finalBitmap, path);

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

            return null;
        }
    }

    private static Bitmap rotateBitmap(Bitmap finalBitmap, String path) {
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int orientation;
        try {
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return finalBitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return finalBitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return finalBitmap;
        }
        try {
            Bitmap bmRotated =
                    Bitmap.createBitmap(finalBitmap, 0, 0, finalBitmap.getWidth(), finalBitmap.getHeight(), matrix, true);
            finalBitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Bitmap mergeBitmaps(Bitmap original, Bitmap overlay) {
        Bitmap result = Bitmap.createBitmap(original.getWidth(), original
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        if (original.getHeight() < original.getWidth())
            overlay = Utils.getOptimizedBitmap(overlay, original.getHeight());
        else
            overlay = Utils.getOptimizedBitmap(overlay, original.getWidth());

        canvas.drawBitmap(original, 0, 0, paint);
        canvas.drawBitmap(overlay, (original.getWidth() - overlay.getWidth()) / 2, (original.getHeight() - overlay.getHeight()) / 2, paint);

        return result;
    }

    public static 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;
    }

    public static boolean isConnectionAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }


    public static boolean isNetworkConnected(Context c) {
        ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null)
        {
            toast(c, R.string.no_connection);
            return false;
        }

         else
            return true;
    }
    public boolean isInternetAvailable(Context c) {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com");

            if (ipAddr.toString().equals(""))
            {
                toast(c, R.string.Internet_no_work);
                return false;
            }

             else
                return true;


        } catch (Exception e) {
            return false;
        }

    }

    public static void toast(Context c,int source) {
        try
        {
            Toast.makeText(c, c.getText(source), Toast.LENGTH_SHORT).show();
        }catch (Exception e){}


//        LayoutInflater inflater = LayoutInflater.from(c);
//        View layout = inflater.inflate(R.layout.toast_layout, null);
//
//        TextView text = (TextView) layout.findViewById(R.id.text);
//        text.setText(c.getString(source));
//
//        Toast toast = new Toast(c.getApplicationContext());
//        toast.setGravity(Gravity.BOTTOM, 0, -100);
//        toast.setDuration(Toast.LENGTH_LONG);
//        toast.setView(layout);
//
//        toast.show();
    }





    }


Commits for Nextrek/Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Manager/Utils.java

Diff revisions: vs.
Revision Author Commited Message
785 Diff Diff FFontana picture FFontana Sat 26 Sep, 2015 14:15:20 +0000
765 Diff Diff MStefanelli picture MStefanelli Tue 22 Sep, 2015 10:03:07 +0000

me

715 Diff Diff MStefanelli picture MStefanelli Tue 08 Sep, 2015 07:59:31 +0000

me

697 Diff Diff FFontana picture FFontana Mon 07 Sep, 2015 12:37:18 +0000
696 Diff Diff FFontana picture FFontana Mon 07 Sep, 2015 10:52:07 +0000
690 Diff Diff MStefanelli picture MStefanelli Fri 04 Sep, 2015 15:46:58 +0000

me

679 Diff Diff FFontana picture FFontana Fri 04 Sep, 2015 14:15:27 +0000
667 Diff Diff FFontana picture FFontana Fri 04 Sep, 2015 10:26:22 +0000
599 Diff Diff MStefanelli picture MStefanelli Fri 28 Aug, 2015 07:16:44 +0000
547 MStefanelli picture MStefanelli Tue 25 Aug, 2015 10:11:57 +0000

me