Example usage for android.app Activity openFileInput

List of usage examples for android.app Activity openFileInput

Introduction

In this page you can find the example usage for android.app Activity openFileInput.

Prototype

@Override
    public FileInputStream openFileInput(String name) throws FileNotFoundException 

Source Link

Usage

From source file:Main.java

private static void shareAct(Activity act, String fileName, String text) {

    Uri uri = null;/*from  w w  w.  j ava 2 s .c  o m*/

    try {
        FileInputStream input = act.openFileInput(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        uri = Uri.parse(MediaStore.Images.Media.insertImage(act.getContentResolver(), bitmap, null, null));
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/jpeg");
    act.startActivity(Intent.createChooser(shareIntent, act.getTitle()));
}

From source file:Main.java

public static boolean startLastActivity(Activity activity) {
    try {// w  w  w.  java 2 s . co m
        BufferedReader reader = new BufferedReader(new InputStreamReader(activity.openFileInput(FILENAME)));
        String nextClassName = reader.readLine();
        reader.close();

        if (null == nextClassName || nextClassName.length() < 3) {
            return false;
        }

        String currClassName = activity.getClass().getName();
        if (currClassName.equals(nextClassName)) {
            return false;
        }

        @SuppressWarnings("unchecked")
        Class<? extends Activity> clazz = (Class<? extends Activity>) Class.forName(nextClassName);
        if (null == clazz) {
            return false;
        }

        Intent i = new Intent(activity, clazz);
        activity.startActivity(i);
        activity.finish();
        reader.close();

        return true;
    } catch (Exception e) {
        Toast.makeText(activity, "startLastActivity: " + e, Toast.LENGTH_LONG).show();
    }
    return false;
}

From source file:dynamite.zafroshops.app.fragment.AllZopsFragment.java

private void setZops(boolean force) {
    Activity activity = getActivity();
    adapter = new AllZopsGridViewAdapter(activity, R.id.gridItem, types);

    final SharedPreferences preferences = activity.getPreferences(0);
    final SharedPreferences.Editor editor = preferences.edit();

    if (!preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        HashMap<String, MobileZop> temp = new HashMap<>();

        types = new ArrayList<>(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()));/*from ww  w .  ja va 2  s. c  o  m*/
        for (MobileZop type : types) {
            String key = type.Type.toString();

            if (!temp.containsKey(key)) {
                temp.put(key, type);
            }
        }
        types = new ArrayList<>(temp.values());
        if (adapter != null) {
            adapter.setObjects(types);
            adapter.notifyDataSetChanged();
        }

        try {
            FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(types);
            oos.close();
            fos.close();
            editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
            editor.commit();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
            try {
                FileInputStream fis = activity.openFileInput(StorageKeys.ZOPCATEGORY_KEY);
                ObjectInputStream ois = new ObjectInputStream(fis);

                types = (ArrayList) ois.readObject();
                ois.close();
                fis.close();

                if (adapter != null) {
                    adapter.setObjects(types);
                    adapter.notifyDataSetChanged();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                new ArrayList<Pair<String, String>>() {
                    {
                        add(new Pair<String, String>("count", "true"));
                    }
                });

        Futures.addCallback(result, new FutureCallback<JsonElement>() {

            @Override
            public void onSuccess(JsonElement result) {
                Activity activity = getActivity();
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    types = new Gson().fromJson(result, new TypeToken<ArrayList<MobileZop>>() {
                    }.getType());
                    try {
                        FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY,
                                Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        oos.writeObject(types);
                        oos.close();
                        fos.close();
                        editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
                        editor.commit();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(types);
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    loader.setVisibility(View.INVISIBLE);
                    zops.setVisibility(View.VISIBLE);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                Activity activity = getActivity();
                if (activity != null && types.size() == 0) {
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    zops.setVisibility(View.INVISIBLE);
                    loader.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}

From source file:dynamite.zafroshops.app.fragment.TypedZopsFragment.java

private void setZops(final boolean force) {
    Activity activity = getActivity();
    adapter = new TypedZopListViewAdapter(getActivity(), R.id.listViewItem, typedZops);
    final SharedPreferences preferences = activity.getPreferences(0);

    final SharedPreferences.Editor editor = preferences.edit();
    final ZopType type = (ZopType) getArguments().get(ARG_ZOP_TYPE);
    final String key = StorageKeys.ZOPS_KEY + type.toString();

    zopType = type;/* ww  w  . j  a v  a  2s .co m*/

    if (!preferences.contains(key)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        typedZops = new ArrayList<>(Collections2.filter(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()), new Predicate<MobileZop>() {
                    @Override
                    public boolean apply(MobileZop input) {
                        return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                    }
                }));

        try {
            FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            int count = typedZops.size();

            oos.writeObject(typedZops);
            oos.close();
            fos.close();
            editor.putString(key, Integer.toString(count));
            editor.commit();
            setCount(count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else if (preferences.contains(key)) {
        try {
            FileInputStream fis = activity.openFileInput(key);
            ObjectInputStream ois = new ObjectInputStream(fis);

            ArrayList<MobileZop> temp = (ArrayList) ois.readObject();
            typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                @Override
                public boolean apply(MobileZop input) {
                    return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                            || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                            || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                }
            }));
            ois.close();
            fis.close();
            setCount(typedZops.size());
            if (adapter != null) {
                adapter.setObjects(typedZops);
                adapter.notifyDataSetChanged();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (force) {
        final ArrayList<Pair<String, String>> parameters;
        if (MainActivity.LastLocation != null) {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("latitude", Double.toString(MainActivity.LastLocation.Latitude)));
                    add(new Pair<>("longitude", Double.toString(MainActivity.LastLocation.Longitude)));
                    add(new Pair<>("type", type.getText()));
                }
            };
        } else {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("type", type.getText()));
                }
            };
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                parameters);

        Futures.addCallback(result, new FutureCallback<JsonElement>() {
            Activity activity = getActivity();

            @Override
            public void onSuccess(JsonElement result) {
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    ArrayList<MobileZop> temp = new Gson().fromJson(result,
                            new TypeToken<ArrayList<MobileZop>>() {
                            }.getType());

                    int max = Collections.max(temp, new Comparator<MobileZop>() {
                        @Override
                        public int compare(MobileZop lhs, MobileZop rhs) {
                            if (lhs.DataVersion < rhs.DataVersion) {
                                return -1;
                            } else if (lhs.DataVersion > rhs.DataVersion) {
                                return 1;
                            } else {
                                return 0;
                            }
                        }
                    }).DataVersion;
                    ((MainActivity) activity).Versions.put(zopType, max);

                    try {
                        FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        int count = typedZops.size();

                        typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                            @Override
                            public boolean apply(MobileZop input) {
                                return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                        || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                        || input.CountryID
                                                .equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                            }
                        }));
                        oos.writeObject(typedZops);
                        oos.close();
                        fos.close();
                        editor.putString(key, Integer.toString(count));
                        editor.commit();
                        setCount(count);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(typedZops);
                    resetVisibility(false);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                ListView zops = (ListView) activity.findViewById(R.id.listViewZops);
                RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                LinearLayout noZops = (LinearLayout) activity.findViewById(R.id.noZops);

                resetVisibility(zops, noZops, loader, false);
            }
        });
    }
}