/*
 * Copyright (C) 2017 Team Horizon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xenonota;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    BottomNavigationView navigation;
    ViewPager viewPager;
    MenuItem prevMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
        navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        viewPager = findViewById(R.id.viewpager);
        viewPager.addOnPageChangeListener(mOnPageChangeListener);
        setupViewPager(viewPager);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            }
            else
            {
                navigation.getMenu().getItem(0).setChecked(false);
            }

            invalidateOptionsMenu();

            navigation.getMenu().getItem(position).setChecked(true);
            prevMenuItem = navigation.getMenu().getItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_rom:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.navigation_gapps:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.navigation_settings:
                    viewPager.setCurrentItem(2);
                    break;
            }
            return true;
        }
    };

    private void setupViewPager(ViewPager viewPager)
    {
        invalidateOptionsMenu();
    }

}

@@ -618,16 +618,108 @@
 onota;%0A%0A
+import android.Manifest;%0Aimport android.content.pm.PackageManager;%0Aimport android.os.Build;%0A
 import a
@@ -736,16 +736,16 @@
 Bundle;%0A
-
 import a
@@ -999,16 +999,45 @@
 enuItem;
+%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -1175,16 +1175,77 @@
 uItem;%0A%0A
+    private static final int STORAGE_PERMISSION_CODE = 200;%0A%0A
     @Ove
@@ -1816,24 +1816,60 @@
 (viewPager);
+%0A%0A        checkStoragePermissions();
 %0A    %7D%0A%0A    
@@ -3644,32 +3644,32 @@
 ;%0A        %7D%0A
-
     %7D;%0A%0A
     private 
@@ -3648,32 +3648,890 @@
       %7D%0A    %7D;%0A%0A
+    private void checkStoragePermissions() %7B%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A            requestPermissions(new String%5B%5D%7B Manifest.permission.WRITE_EXTERNAL_STORAGE%7D,%0A                    STORAGE_PERMISSION_CODE);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode, @NonNull String%5B%5D permissions,%0A                                           @NonNull int%5B%5D grantResults) %7B%0A        super.onRequestPermissionsResult(requestCode, permissions, grantResults);%0A        if (requestCode == STORAGE_PERMISSION_CODE && grantResults.length %3E 0%0A                && grantResults%5B0%5D == PackageManager.PERMISSION_GRANTED) %7B%0A        %7D else %7B%0A            Toast.makeText(MainActivity.this,  R.string.permission_not_enabled, Toast.LENGTH_LONG)%0A                    .show();%0A            finish();%0A        %7D%0A    %7D%0A%0A
     private void

Check Storage Permission