Android Open Source - prepGRE Home Fragment






From Project

Back to project page prepGRE.

License

The source code is released under:

MIT License

If you think the Android project prepGRE listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.abhimanbhau.prepgre.ui;
/* w  ww. j a v  a  2 s.c o m*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

import com.abhimanbhau.prepgre.R;
import com.abhimanbhau.prepgre.code.EtymologyRetriever;
import com.abhimanbhau.prepgre.code.MnemonicRetriever;
import com.abhimanbhau.prepgre.code.Utility;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import de.keyboardsurfer.android.widget.crouton.Crouton;

public class HomeFragment extends Fragment {
    AutoCompleteTextView searchBox;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        searchBox = (AutoCompleteTextView) view
                .findViewById(R.id.autoSearchBox);
        tv = (TextView) view.findViewById(R.id.txtMainDataView);

        Button btnMnemonic = (Button) view.findViewById(R.id.btnMnemonic);
        Button btnEtymology = (Button) view.findViewById(R.id.btnEtymology);

        btnMnemonic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Utility.hideKeyBoard(getActivity());
                String result = Utility.checkCacheAndRetrieve(searchBox
                        .getText().toString().trim(), true, getActivity());
                if (result != null) {
                    tv.setText(result);
                } else if (!checkNetworkConnectionAndShowErrorMessage()) {
                    return;
                } else if (searchBox.getText().toString().trim().equals("")) {
                    Utility.makeCrouton(1, getActivity());
                    return;
                } else {
                    try {
                        Utility.makeCrouton(111, getActivity());
                        result = new MnemonicRetriever()
                                .execute(
                                        "http://mnemonicdictionary.com/word/"
                                                + searchBox.getText()
                                                .toString().trim())
                                .get();
                        if (result == null) {
                            Utility.makeCrouton(2, getActivity());
                        } else {
                            tv.setText(result);
                            saveToCache(result, true, searchBox.getText()
                                    .toString().trim());
                        }
                    } catch (Exception e) {
                        Utility.makeCrouton(3, getActivity());
                    }
                }
            }
        });

        btnEtymology.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Utility.hideKeyBoard(getActivity());
                String result = Utility.checkCacheAndRetrieve(searchBox
                        .getText().toString().trim(), false, getActivity());
                if (result != null) {
                    tv.setText(result);
                } else if (!checkNetworkConnectionAndShowErrorMessage()) {
                    return;
                } else if (searchBox.getText().toString().trim().equals("")) {
                    Utility.makeCrouton(1, getActivity());
                    return;
                } else {
                    Utility.makeCrouton(111, getActivity());
                    try {
                        result = new EtymologyRetriever().execute(
                                "http://www.etymonline.com/index.php?term="
                                        + searchBox.getText().toString().trim()
                                        + "&allowed_in_frame=0").get();
                        if (result == null) {
                            Utility.makeCrouton(2, getActivity());
                        } else {
                            tv.setText(result);
                            saveToCache(result, false, searchBox.getText()
                                    .toString().trim());
                        }
                    } catch (Exception e) {
                        Utility.makeCrouton(3, getActivity());
                    }
                }
            }
        });
        try {
            searchBox.setAdapter(getSearchSuggestionsAdapter());
        } catch (Exception e) {
            e.printStackTrace();
        }
        searchBox.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Utility.hideKeyBoard(getActivity());
            }
        });
        Bundle b = getArguments();
        if (b != null) {
            searchBox.setText(getArguments().getString("Word"));
        }
        if (savedInstanceState != null) {
            searchBox.setText(savedInstanceState.getString("Word"));
        }
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        searchBox = (AutoCompleteTextView) getActivity().findViewById(
                R.id.autoSearchBox);
        outState.putString("Word", searchBox.getText().toString().trim());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Crouton.cancelAllCroutons();
    }

    // Utility/Helper Methods

    private ArrayAdapter<String> getSearchSuggestionsAdapter() {
        try {
            String line = "";
            List<String> lines = new ArrayList<String>();
            InputStream is = getResources().getAssets().open("wordlist.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            String[] arr = lines.toArray(new String[lines.size()]);
            ArrayAdapter<String> a = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_dropdown_item_1line, arr);
            return a;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayAdapter<String>(getActivity(), 0);
    }

    public void saveToCache(String data, boolean isMnemonic, String word) {
        String fileName;
        if (isMnemonic) {
            fileName = word + "-m";
        } else {
            fileName = word + "-e";
        }
        try {
            FileOutputStream out = getActivity().openFileOutput(fileName,
                    Context.MODE_PRIVATE);
            byte[] ff = data.getBytes();
            for (int i = 0; i < ff.length; ++i) {
                if (ff[i] == 10) {
                    ff[i] = 96;
                }
            }
            out.write(ff);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean checkNetworkConnectionAndShowErrorMessage() {
        ConnectivityManager connectivity = (ConnectivityManager) getActivity()
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        Utility.makeCrouton(4, getActivity());
        return false;
    }
}




Java Source Code List

com.abhimanbhau.prepgre.code.EtymologyRetriever.java
com.abhimanbhau.prepgre.code.MnemonicRetriever.java
com.abhimanbhau.prepgre.code.Utility.java
com.abhimanbhau.prepgre.ui.AboutFragment.java
com.abhimanbhau.prepgre.ui.HomeFragment.java
com.abhimanbhau.prepgre.ui.MainActivity.java
com.abhimanbhau.prepgre.ui.NavigationDrawerFragment.java
com.abhimanbhau.prepgre.ui.SettingsActivity.java
com.abhimanbhau.prepgre.ui.WordListFragment.java