Advertisement
ethancannon10

Fragment

Jun 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.29 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     public static ArrayAdapter mAdapter;
  4.     DrawerLayout mDrawerLayout;
  5.     ActionBarDrawerToggle mDrawerToggle;
  6.     public static ListView mDrawerList;
  7.     Toolbar toolbar;
  8.     Fragment fragment;
  9.  
  10.     static String[] drawerTitles;
  11.     static int[] icons = {R.drawable.drawer_all,R.drawable.drawer_new,R.drawable.drawer_favorites, 0, R.drawable.drawer_material,R.drawable.drawer_landscape,R.drawable.drawer_nature, R.drawable.drawer_sea, R.drawable.drawer_city, R.drawable.drawer_vintage};
  12.  
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         toolbar = (Toolbar) findViewById(R.id.toolbar);
  18.         setSupportActionBar(toolbar);
  19.         mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
  20.         mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
  21.         setDrawer();
  22.  
  23.         SharedPreferences sharedPreferences = new SharedPreferences(getApplicationContext());
  24.         sharedPreferences.saveBoolean("check_mobile_data", true);
  25.  
  26.         if (!BuildConfig.DEBUG)
  27.         //checkLicense();
  28.  
  29.         onClick(0);
  30.     }
  31.  
  32.  
  33.     public static void updateDrawer(Context context) {
  34.         mAdapter = new DrawerAdapter(context, drawerTitles, icons, getData(context));
  35.         mDrawerList.setAdapter(mAdapter);
  36.     }
  37.  
  38.     private void setDrawer() {
  39.         mDrawerList = (ListView) findViewById(R.id.drawer_list_view);
  40.         drawerTitles = getResources().getStringArray(R.array.drawer_items);
  41.         mAdapter = new DrawerAdapter(getApplicationContext(), drawerTitles, icons, getData(getApplicationContext()));
  42.         View header = getLayoutInflater().inflate(R.layout.drawer_header, null);
  43.         mDrawerList.addHeaderView(header, null, false);
  44.         mDrawerList.setAdapter(mAdapter);
  45.         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, 0, 0) {
  46.  
  47.             @Override
  48.             public void onDrawerOpened(View drawerView) {
  49.                 super.onDrawerOpened(drawerView);
  50.             }
  51.  
  52.             @Override
  53.             public void onDrawerClosed(View drawerView) {
  54.                 super.onDrawerClosed(drawerView);
  55.             }
  56.         };
  57.         mDrawerLayout.setDrawerListener(mDrawerToggle);
  58.         mDrawerToggle.syncState();
  59.         mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  60.  
  61.             @Override
  62.             public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
  63.                 onClick(position - 1);
  64.             }
  65.         });
  66.  
  67.     }
  68.  
  69.  
  70.     private void onClick(int position) {
  71.         if (position != 3) {
  72.             RecyclerAdapter.categoryChanged(getApplicationContext());
  73.             fragment = Fragment.newInstance(position);
  74.             FragmentManager fragmentManager = getSupportFragmentManager();
  75.             fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
  76.             setTitle(drawerTitles[position]);
  77.             DrawerAdapter.setSelectedItem(position);
  78.             mAdapter.notifyDataSetChanged();
  79.             mDrawerLayout.closeDrawers();
  80.         }
  81.     }
  82.  
  83.     public static int[] getData(Context context) {
  84.         ArrayList<String> title = new ArrayList<>();
  85.         String file = readJSON(context);
  86.         int[] count = new int[10];
  87.         try {
  88.             JSONObject jsonObject = new JSONObject(file);
  89.             JSONArray jsonArray = jsonObject.getJSONArray("categories");
  90.             for (int i = 0; i < jsonArray.length(); i++) {
  91.                 jsonObject = jsonArray.getJSONObject(i);
  92.                 JSONArray jsonArrayWallpaper = jsonObject.getJSONArray("wallpaper");
  93.                 count[i+4] = jsonArrayWallpaper.length();
  94.                 for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  95.                     title.add(jsonArrayWallpaper.getJSONObject(ii).getString("title"));
  96.                 }
  97.             }
  98.             count[0] = count[4] + count[5] + count[6] + count[7] + count[8] + count[9];
  99.  
  100.             int version = 0;
  101.             try {
  102.                 version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
  103.             }
  104.             catch (PackageManager.NameNotFoundException ex) {
  105.                 ex.printStackTrace();
  106.             }
  107.             jsonObject = new JSONObject(file);
  108.             jsonArray = jsonObject.getJSONArray("categories");
  109.             for (int i = 0; i < jsonArray.length(); i++) {
  110.                 jsonObject = jsonArray.getJSONObject(i);
  111.                 JSONArray jsonArrayWallpaper = jsonObject.getJSONArray("wallpaper");
  112.  
  113.                 for (int ii = 0; ii < jsonArrayWallpaper.length(); ii++) {
  114.                     if (jsonArrayWallpaper.getJSONObject(ii).getInt("version_added") == version) {
  115.                         count[1] += 1;
  116.                     }
  117.                 }
  118.             }
  119.  
  120.             SharedPreferences sharedPreferences = new SharedPreferences(context);
  121.             for (int i = 0; i < title.size(); i++) {
  122.                 if (sharedPreferences.getBoolean(title.get(i).toLowerCase().replaceAll(" ", "_").trim(), false)) {
  123.                     count[2] += 1;
  124.                 }
  125.             }
  126.         }
  127.         catch (JSONException ex) {
  128.             ex.printStackTrace();
  129.         }
  130.         if (mAdapter != null) {
  131.             mDrawerList.setAdapter(mAdapter);
  132.             mAdapter.notifyDataSetChanged();
  133.             mDrawerList.deferNotifyDataSetChanged();
  134.         }
  135.         return count;
  136.     }
  137.  
  138.  
  139.     public static String readJSON(Context context) {
  140.         try {
  141.             InputStream is = context.getAssets().open("wallpaper.json");
  142.             int size = is.available();
  143.             byte[] buffer = new byte[size];
  144.             is.read(buffer);
  145.             is.close();
  146.             return new String(buffer, "UTF-8");
  147.         } catch (IOException ex) {
  148.             ex.printStackTrace();
  149.         }
  150.         return "";
  151.     }
  152.  
  153.  
  154.     private void checkLicense() {
  155.         String installer = getPackageManager().getInstallerPackageName(getPackageName());
  156.         Log.d("Installer", "Installer: " + installer);
  157.             if (installer == null || !installer.equals("com.android.vending")) {
  158.                 AlertDialog.Builder alertDialogBuilder =
  159.                     new AlertDialog.Builder(this)
  160.                             .setTitle(R.string.license)
  161.                             .setMessage(R.string.license_unsuccessful)
  162.                             .setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
  163.                                 public void onClick(DialogInterface dialog, int which) {
  164.                                     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
  165.                                     startActivity(browserIntent);
  166.                                     dialog.dismiss();
  167.                                     finish();
  168.                                 }
  169.                             })
  170.                         .setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
  171.                             @Override
  172.                             public void onClick(DialogInterface dialog, int which) {
  173.                                 finish();
  174.                             }
  175.                         })
  176.                         .setOnCancelListener(new DialogInterface.OnCancelListener() {
  177.                             @Override
  178.                             public void onCancel(DialogInterface dialog) {
  179.                                 finish();
  180.                             }
  181.                         });
  182.                 alertDialogBuilder.show();
  183.             }
  184.     }
  185.  
  186.  
  187.     @Override
  188.     public boolean onCreateOptionsMenu(Menu menu) {
  189.         getMenuInflater().inflate(R.menu.menu_main, menu);
  190.         return super.onCreateOptionsMenu(menu);
  191.     }
  192.  
  193.     @Override
  194.     public boolean onOptionsItemSelected(final MenuItem item) {
  195.         switch (item.getItemId()) {
  196.             case R.id.action_libraries:
  197.                 AlertDialog.Builder alertDialogBuilder =
  198.                         new AlertDialog.Builder(MainActivity.this)
  199.                                 .setTitle(R.string.dialog_copyright_notice)
  200.                                 .setMessage(R.string.dialog_copyright_text)
  201.                                 //.setIcon(R.drawable.ic_copyright_black_24dp)
  202.                                 .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
  203.                                     public void onClick(DialogInterface dialog, int which) {
  204.                                         dialog.dismiss();
  205.                                     }
  206.                                 });
  207.                 alertDialogBuilder.show();
  208.                 return true;
  209.             /*case R.id.action_google_plus:
  210.                 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/u/0/communities/103161045714462511515"));
  211.                 startActivity(browserIntent);
  212.                 return true;*/
  213.             case R.id.action_changelog:
  214.                 AlertDialog.Builder changelogDialogBuilder =
  215.                         new AlertDialog.Builder(MainActivity.this)
  216.                                 .setTitle(R.string.dialog_changelog)
  217.                                 .setMessage(R.string.changelog_text)
  218.                                // .setIcon(R.drawable.ic_new_releases_black_24dp)
  219.                                 .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
  220.                                     public void onClick(DialogInterface dialog, int which) {
  221.                                         dialog.dismiss();
  222.                                     }
  223.                                 });
  224.                 changelogDialogBuilder.show();
  225.                 return true;
  226.             case R.id.action_about:
  227.                 AlertDialog.Builder aboutDialogBuilder =
  228.                         new AlertDialog.Builder(MainActivity.this)
  229.                                 .setTitle(R.string.dialog_about)
  230.                                 .setMessage(R.string.dialog_about_text)
  231.                               //  .setIcon(R.drawable.ic_info_outline_black_24dp)
  232.                                 .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
  233.                                     public void onClick(DialogInterface dialog, int which) {
  234.                                         dialog.dismiss();
  235.                                     }
  236.                                 });
  237.                 aboutDialogBuilder.show();
  238.                 return true;
  239.  
  240.             default:
  241.                 break;
  242.         }
  243.         return false;
  244.     }
  245.     protected void onStart()
  246.     {
  247.         super.onStart();
  248.         Batch.onStart(this);
  249.     }
  250.  
  251.     @Override
  252.     protected void onStop()
  253.     {
  254.         Batch.onStop(this);
  255.  
  256.         super.onStop();
  257.     }
  258.  
  259.     @Override
  260.     protected void onDestroy()
  261.     {
  262.         Batch.onDestroy(this);
  263.  
  264.         super.onDestroy();
  265.     }
  266.  
  267.     @Override
  268.     protected void onNewIntent(Intent intent)
  269.     {
  270.         Batch.onNewIntent(this, intent);
  271.  
  272.         super.onNewIntent(intent);
  273.     }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement