package io.github.jokoframework.otp;


import android.Manifest;
import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.DataSetObserver;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;

import io.github.jokoframework.R;
import io.github.jokoframework.otp.add.ScanActivity;
import io.github.jokoframework.otp.token.TokenAdapter;


public class OtpActivity extends Activity{
    private TokenAdapter mTokenAdapter;
    public static final String ACTION_IMAGE_SAVED = "io.github.jokoframework.otp.ACTION_IMAGE_SAVED";
    private DataSetObserver mDataSetObserver;
    private final int PERMISSIONS_REQUEST_CAMERA = 1;
    private RefreshListBroadcastReceiver receiver;

    private class RefreshListBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            mTokenAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp);
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        mTokenAdapter = new TokenAdapter(this);
        receiver = new RefreshListBroadcastReceiver();
        registerReceiver(receiver, new IntentFilter(ACTION_IMAGE_SAVED));
        ((GridView) findViewById(R.id.grid)).setAdapter(mTokenAdapter);

        // Don't permit screenshots since these might contain OTP codes.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        mDataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (mTokenAdapter.getCount() == 0)
                    findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
                else
                    findViewById(android.R.id.empty).setVisibility(View.GONE);
            }
        };
        mTokenAdapter.registerDataSetObserver(mDataSetObserver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTokenAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTokenAdapter.notifyDataSetChanged();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.otp_main, menu);
        menu.findItem(R.id.action_scan).setVisible(ScanActivity.hasCamera(this));
        menu.findItem(R.id.action_scan).setOnMenuItemClickListener(this::onOptionsItemSelected);
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void tryOpenCamera() {
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
        }
        else {
            // permission is already granted
            openCamera();
        }
    }

    private void openCamera() {
        startActivity(new Intent(this, ScanActivity.class));
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /**
         * Se obtiene el id del item del action bar seleccionado
         * y se realiza la acción de acuerdo a éste
         */
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        else if (item.getItemId() == R.id.action_scan){
            tryOpenCamera();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

@@ -31,17 +31,16 @@
 k.otp;%0A%0A
-%0A
 import a
@@ -60,38 +60,8 @@
 st;%0A
-import android.app.ActionBar;%0A
 impo
@@ -379,150 +379,88 @@
 roid
-x.annotation.RequiresApi;%0Aimport android.view.Menu;%0Aimport android.view.MenuInflater;%0Aimport android.view.MenuItem;%0Aimport android.view.
+.view.View;%0Aimport android.view.WindowManager;%0Aimport android.widget.Grid
 View;%0A
+%0A
 impo
@@ -473,61 +473,68 @@
 roid
-.view.WindowManager;%0Aimport android.widget.GridView;%0A
+x.annotation.RequiresApi;%0A%0Aimport io.github.jokoframework.R;
 %0Aimp
@@ -553,33 +553,54 @@
 b.jokoframework.
-R
+activity.Home2Activity
 ;%0Aimport io.gith
@@ -888,116 +888,8 @@
     
-private DataSetObserver mDataSetObserver;%0A    private final int PERMISSIONS_REQUEST_CAMERA = 1;%0A    private 
 Refr
@@ -921,16 +921,53 @@
 receiver
+ = new RefreshListBroadcastReceiver()
 ;%0A%0A    p
@@ -1177,32 +1177,78 @@
        %7D%0A    %7D%0A%0A
+    @RequiresApi(api = Build.VERSION_CODES.M)%0A
     @Override%0A  
@@ -1393,24 +1393,25 @@
 y_otp);%0A
+%0A
         
 ActionBa
@@ -1406,131 +1406,355 @@
     
-ActionBar actionBar = getActionBar();%0A        if (actionBar != null) %7B%0A            actionBar.setDisplayHomeAsUpEnabled(true
+findViewById(R.id.scanImage).setOnClickListener(v -%3E tryOpenCamera());%0A%0A        findViewById(R.id.back1).setOnClickListener(v -%3E %7B%0A            Intent intent = new Intent(OtpActivity.this, Home2Activity.class);%0A            startActivity(intent);%0A            finish();%0A            overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left
 );%0A 
@@ -1753,32 +1753,35 @@
 left);%0A        %7D
+);%0A
 %0A        mTokenA
@@ -1817,63 +1817,8 @@
 s);%0A
-        receiver = new RefreshListBroadcastReceiver();%0A
     
@@ -2152,16 +2152,32 @@
         
+DataSetObserver 
 mDataSet
@@ -2888,383 +2888,8 @@
  %7D%0A%0A
-%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        MenuInflater inflater = getMenuInflater();%0A        inflater.inflate(R.menu.otp_main, menu);%0A        menu.findItem(R.id.action_scan).setVisible(ScanActivity.hasCamera(this));%0A        menu.findItem(R.id.action_scan).setOnMenuItemClickListener(this::onOptionsItemSelected);%0A        return true;%0A    %7D%0A%0A
     
@@ -3061,24 +3061,72 @@
 _GRANTED) %7B%0A
+            int PERMISSIONS_REQUEST_CAMERA = 1;%0A
             
@@ -3497,554 +3497,6 @@
   %7D%0A
-%0A%0A    @RequiresApi(api = Build.VERSION_CODES.M)%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        /**%0A         * Se obtiene el id del item del action bar seleccionado%0A         * y se realiza la acci%C3%B3n de acuerdo a %C3%A9ste%0A         */%0A        if (item.getItemId() == android.R.id.home) %7B%0A            finish();%0A            return true;%0A        %7D%0A        else if (item.getItemId() == R.id.action_scan)%7B%0A            tryOpenCamera();%0A            return true;%0A        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A
 %7D%0A

Fix de los errores y metodos obsoletos removidos.