Advertisement
ethancannon10

Fragment2

Jun 6th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. public class Fragment extends android.support.v4.app.Fragment {
  2.  
  3.     static int position;
  4.  
  5.     ArrayList<String> title = new ArrayList<>();
  6.     ArrayList<String> icon = new ArrayList<>();
  7.     ArrayList<String> author = new ArrayList<>();
  8.  
  9.     public Fragment() {
  10.         // Required empty public constructor
  11.     }
  12.  
  13.     public static Fragment newInstance(int drawerPosition) {
  14.         position = drawerPosition;
  15.         return new Fragment();
  16.     }
  17.  
  18.  
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         checkOnline();
  23.     }
  24.  
  25.     @Override
  26.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  27.                              Bundle savedInstanceState) {
  28.         View v = inflater.inflate(R.layout.fragment_wallpaper, container, false);
  29.         RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
  30.         title.clear();
  31.         icon.clear();
  32.         author.clear();
  33.         getData(position);
  34.         RecyclerAdapter recyclerAdapter = new RecyclerAdapter(getActivity(), title, icon, author);
  35.         GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
  36.         recyclerView.setLayoutManager(gridLayoutManager);
  37.  
  38.         recyclerView.setAdapter(recyclerAdapter);
  39.         recyclerAdapter.notifyDataSetChanged();
  40.         return v;
  41.     }
  42.  
  43.     private void getData(int position) {
  44.         String file = readJSON();
  45.  
  46.         try {
  47.             // All
  48.             if (position == 0) {
  49.                 JSONObject jsonObject = new JSONObject(file);
  50.                 JSONArray jsonArray = jsonObject.getJSONArray("categories");
  51.                 for (int i = 0; i < jsonArray.length(); i++) {
  52.                     jsonObject = jsonArray.getJSONObject(i);
  53.                     JSONArray jsonArrayWallpaper = jsonObject.getJSONArray("wallpaper");
  54.  
  55.                     for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  56.                         icon.add(jsonArrayWallpaper.getJSONObject(ii).getString("url"));
  57.                         title.add(jsonArrayWallpaper.getJSONObject(ii).getString("title"));
  58.                         author.add(jsonArrayWallpaper.getJSONObject(ii).getString("author"));
  59.                     }
  60.                 }
  61.                 return;
  62.             }
  63.  
  64.             // New
  65.             else if (position == 1) {
  66.                 int version = 0;
  67.                 try {
  68.                     version = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionCode;
  69.                 }
  70.                 catch (PackageManager.NameNotFoundException ex) {
  71.                     ex.printStackTrace();
  72.                 }
  73.                 JSONObject jsonObject = new JSONObject(file);
  74.                 JSONArray jsonArray = jsonObject.getJSONArray("categories");
  75.                 for (int i = 0; i < jsonArray.length(); i++) {
  76.                     jsonObject = jsonArray.getJSONObject(i);
  77.                     JSONArray jsonArrayWallpaper = jsonObject.getJSONArray("wallpaper");
  78.  
  79.                     for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  80.                         if (jsonArrayWallpaper.getJSONObject(ii).getInt("version_added") == version) {
  81.                             icon.add(jsonArrayWallpaper.getJSONObject(ii).getString("url"));
  82.                             title.add(jsonArrayWallpaper.getJSONObject(ii).getString("title"));
  83.                             author.add(jsonArrayWallpaper.getJSONObject(ii).getString("author"));
  84.                         }
  85.                     }
  86.                 }
  87.                 return;
  88.             }
  89.  
  90.             // Favorites
  91.             else if (position == 2) {
  92.                 JSONObject jsonObject = new JSONObject(file);
  93.                 JSONArray jsonArray = jsonObject.getJSONArray("categories");
  94.                 SharedPreferences sharedPreferences = new SharedPreferences(getActivity());
  95.                 for (int i = 0; i < jsonArray.length(); i++) {
  96.                     jsonObject = jsonArray.getJSONObject(i);
  97.                     JSONArray jsonArrayWallpaper = jsonObject.getJSONArray("wallpaper");
  98.  
  99.                     for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  100.                         if (sharedPreferences.getBoolean(jsonArrayWallpaper.getJSONObject(ii).getString("title").toLowerCase().replaceAll(" ", "_").trim(), false)) {
  101.                             icon.add(jsonArrayWallpaper.getJSONObject(ii).getString("url"));
  102.                             title.add(jsonArrayWallpaper.getJSONObject(ii).getString("title"));
  103.                             author.add(jsonArrayWallpaper.getJSONObject(ii).getString("author"));
  104.                         }
  105.                     }
  106.                 }
  107.                 return;
  108.             }
  109.  
  110.             // All other categories
  111.             else {
  112.                 JSONObject jsonObject = new JSONObject(file);
  113.                 JSONArray jsonArray = jsonObject.getJSONArray("categories");
  114.                 jsonObject = jsonArray.getJSONObject(position - 4);
  115.                 jsonArray = jsonObject.getJSONArray("wallpaper");
  116.  
  117.                 for (int i = 0; i < jsonArray.length(); i++) {
  118.                     icon.add(i, jsonArray.getJSONObject(i).getString("url"));
  119.                     title.add(i, jsonArray.getJSONObject(i).getString("title"));
  120.                     author.add(i, jsonArray.getJSONObject(i).getString("author"));
  121.                 }
  122.             }
  123.         }
  124.         catch (JSONException ex) {
  125.             ex.printStackTrace();
  126.         }
  127.     }
  128.  
  129.     private String readJSON() {
  130.         try {
  131.             InputStream is = getActivity().getAssets().open("wallpaper.json");
  132.             int size = is.available();
  133.             byte[] buffer = new byte[size];
  134.             is.read(buffer);
  135.             is.close();
  136.             return new String(buffer, "UTF-8");
  137.         } catch (IOException ex) {
  138.             ex.printStackTrace();
  139.         }
  140.         return "";
  141.     }
  142.  
  143.     private void checkOnline() {
  144.         try {
  145.  
  146.             final ConnectivityManager connMgr = (ConnectivityManager)
  147.                     getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
  148.             final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  149.             final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  150.  
  151.             if (mobile.isConnectedOrConnecting()) {
  152.                 final SharedPreferences sharedPreferences = new SharedPreferences(getActivity());
  153.                 if (sharedPreferences.getBoolean("check_mobile_data", true)) {
  154.                     AlertDialog.Builder alertDialogBuilder =
  155.                             new AlertDialog.Builder(getActivity())
  156.                                     .setTitle(R.string.mobile_data)
  157.                                     .setMessage(R.string.mobile_data_warning)
  158.                                     .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
  159.                                         public void onClick(DialogInterface dialog, int which) {
  160.                                             dialog.dismiss();
  161.                                             sharedPreferences.saveBoolean("check_mobile_data", false);
  162.                                         }
  163.                                     });
  164.                     alertDialogBuilder.show();
  165.                 }
  166.             } else if (!wifi.isConnectedOrConnecting()) {
  167.                 AlertDialog.Builder alertDialogBuilder =
  168.                         new AlertDialog.Builder(getActivity())
  169.                                 .setTitle(R.string.no_connection)
  170.                                 .setMessage(R.string.connection_error_message)
  171.                                 .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
  172.                                     public void onClick(DialogInterface dialog, int which) {
  173.                                         dialog.dismiss();
  174.                                     }
  175.                                 });
  176.                 alertDialogBuilder.show();
  177.             }
  178.         }
  179.         catch (Exception e)
  180.         {
  181.  
  182.         }
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement