com.mksingh.camfone.Pinlock.ConfirmPinActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.mksingh.camfone.Pinlock.ConfirmPinActivity.java

Source

/*
 * Copyright (C) 2015. Manu Sunny <manupsunny@gmail.com>
 *
 * 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.mksingh.camfone.Pinlock;

import android.os.Bundle;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mksingh.camfone.R;
import com.mksingh.camfone.Retrofit.ApiClient;
import com.mksingh.camfone.Retrofit.ApiEndpointInterface;
import com.mksingh.camfone.Utils.AppConstants;
import com.mksingh.camfone.Utils.AppPreferences;
import com.mksingh.camfone.Utils.DialogManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Abstract class for PIN confirm activity.
 * Subclass this activity to show ConfirmPin screen.
 * All subclasses should implement isPinCorrect() method
 * @since 1.0.0
 */
public abstract class ConfirmPinActivity extends BasePinActivity {

    private DialogManager dialogManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLabel(getString(R.string.message_enter_pin));
        dialogManager = new DialogManager();
    }

    /**
     * Implementation of BasePinActivity method
     * @param pin PIN value entered by user
     */
    @Override
    public final void onCompleted(String pin) {
        resetStatus();
        if (isPinCorrect(pin)) {
            LoginWithPin(pin);
            /* Intent intent = new Intent();
             intent.putExtra("pin", pin);
             setResult(SUCCESS, intent);
             finish();*/
        } else {
            setLabel(getString(R.string.message_invalid_pin));
        }
    }

    /**
     * Abstract method which decides the PIN entered by user is correct or not
     * @param pin PIN value entered by user
     * @return Boolean value indicates the status of PIN entered
     */
    public abstract boolean isPinCorrect(String pin);

    /**
     * Abstract method which handles PIN forgot scenario
     */
    @Override
    public abstract void onForgotPin();

    @Override
    public void onBackPressed() {
        setResult(CANCELLED);
        finish();
    }

    private void LoginWithPin(String pin) {
        dialogManager.showProcessDialog(this, "");
        JsonObject jsonObject = new JsonObject();

        jsonObject.addProperty("method", AppConstants.CUSTOMER_APIS.LOGINPIN);
        jsonObject.addProperty("email_id", AppPreferences.getUserEmailid(ConfirmPinActivity.this));
        jsonObject.addProperty("loginPin", pin);
        JsonArray jsonArray = new JsonArray();
        JsonElement jsonElement = jsonObject.getAsJsonObject();
        jsonArray.add(jsonElement);
        com.mksingh.camfone.Utils.Log.d("Request pinlogin", jsonArray.getAsJsonArray() + "");

        ApiEndpointInterface apiInterface = ApiClient.getClient(this, "").create(ApiEndpointInterface.class);
        Call<JsonObject> call1 = apiInterface.customerApi(jsonArray);
        call1.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                com.mksingh.camfone.Utils.Log.d("Response pinlogin>>", response.body().toString());

                if (response.body().get("status").getAsString().equalsIgnoreCase("200")) {

                    //String userId = response.body().getAsJsonObject("result").get("user_id").getAsString();

                    //AppPreferences.setUserid(ConfirmPinActivity.this, userId);
                    //startActivity(new Intent(ConfirmPinActivity.this, HomeActivity.class));
                    setResult(SUCCESS);
                    finish();

                } else {
                    Toast.makeText(ConfirmPinActivity.this, response.body().get("message").getAsString(),
                            Toast.LENGTH_LONG).show();
                }
                dialogManager.stopProcessDialog();
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                t.printStackTrace();
                com.mksingh.camfone.Utils.Log.d("onFailure", call.toString() + "\n" + t.getMessage());
                dialogManager.stopProcessDialog();
            }
        });
    }
}