/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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.afwsamples.testdpc.provision;

import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MODE;
import static android.app.admin.DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
import static android.app.admin.DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE;

import android.accounts.Account;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import com.afwsamples.testdpc.AddAccountActivity;
import com.afwsamples.testdpc.R;
import com.afwsamples.testdpc.common.LaunchIntentUtil;
import com.android.setupwizardlib.GlifLayout;

/**
 * Activity that gets launched by the
 * {@link android.app.admin.DevicePolicyManager#ACTION_GET_PROVISIONING_MODE} intent.
 */
public class DpcLoginActivity extends Activity {

    private static final String LOG_TAG = "DpcLoginActivity";
    private static final int ADD_ACCOUNT_REQUEST_CODE = 1;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_dpc_login);
        GlifLayout layout = findViewById(R.id.dpc_login);
        layout.findViewById(R.id.suw_navbar_next).setOnClickListener(this::onNavigateNext);
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ADD_ACCOUNT_REQUEST_CODE:
                finishWithIntent(createResultIntentFromData(data));
                break;
            default:
                Log.d(LOG_TAG, "Unknown result code: " + resultCode);
                break;
        }
    }

    private Intent createResultIntentFromData(Intent data) {
        final Intent resultIntent = new Intent();
        resultIntent.putExtra(EXTRA_PROVISIONING_MODE, PROVISIONING_MODE_MANAGED_PROFILE);
        if (data != null && data.hasExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE)) {
            final Account accountToMigrate = data.getParcelableExtra(
                EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE);
            resultIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, accountToMigrate);
            final PersistableBundle bundle = new PersistableBundle();
            bundle.putString(LaunchIntentUtil.EXTRA_ACCOUNT_NAME, accountToMigrate.name);
            resultIntent.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, bundle);
        }
        return resultIntent;
    }

    private void onNavigateNext(View nextButton) {
        final Intent intent = new Intent();
        RadioGroup dpcLoginOptions = findViewById(R.id.dpc_login_options);
        switch (dpcLoginOptions.getCheckedRadioButtonId()) {
            case R.id.dpc_login_do:
                intent.putExtra(EXTRA_PROVISIONING_MODE, PROVISIONING_MODE_FULLY_MANAGED_DEVICE);
                finishWithIntent(intent);
                return;
            case R.id.dpc_login_po:
                startActivityForResult(
                    new Intent(getApplicationContext(), AddAccountActivity.class),
                    ADD_ACCOUNT_REQUEST_CODE);
                return;
            default:
                finish();
        }
    }

    private void finishWithIntent(Intent intent) {
        setResult(RESULT_OK, intent);
        finish();
    }
}

@@ -661,191 +661,8 @@
 n;%0A%0A
-import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;%0Aimport static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;%0A
 impo
@@ -918,41 +918,8 @@
 E;%0A%0A
-import android.accounts.Account;%0A
 impo
@@ -1004,70 +1004,8 @@
 le;%0A
-import android.os.PersistableBundle;%0Aimport android.util.Log;%0A
 impo
@@ -1094,114 +1094,9 @@
 dpc.
-AddAccountActivity;%0Aimport com.afwsamples.testdpc.R;%0Aimport com.afwsamples.testdpc.common.LaunchIntentUtil
+R
 ;%0Aim
@@ -1326,130 +1326,8 @@
  %7B%0A%0A
-    private static final String LOG_TAG = %22DpcLoginActivity%22;%0A    private static final int ADD_ACCOUNT_REQUEST_CODE = 1;%0A%0A
     
@@ -1746,1195 +1746,8 @@
  %7D%0A%0A
-    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        switch (requestCode) %7B%0A            case ADD_ACCOUNT_REQUEST_CODE:%0A                finishWithIntent(createResultIntentFromData(data));%0A                break;%0A            default:%0A                Log.d(LOG_TAG, %22Unknown result code: %22 + resultCode);%0A                break;%0A        %7D%0A    %7D%0A%0A    private Intent createResultIntentFromData(Intent data) %7B%0A        final Intent resultIntent = new Intent();%0A        resultIntent.putExtra(EXTRA_PROVISIONING_MODE, PROVISIONING_MODE_MANAGED_PROFILE);%0A        if (data != null && data.hasExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE)) %7B%0A            final Account accountToMigrate = data.getParcelableExtra(%0A                EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE);%0A            resultIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, accountToMigrate);%0A            final PersistableBundle bundle = new PersistableBundle();%0A            bundle.putString(LaunchIntentUtil.EXTRA_ACCOUNT_NAME, accountToMigrate.name);%0A            resultIntent.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, bundle);%0A        %7D%0A        return resultIntent;%0A    %7D%0A%0A
     
@@ -2229,159 +2229,124 @@
     
-startActivityForResult(%0A                    new Intent(getApplicationContext(), AddAccountActivity.class),%0A                    ADD_ACCOUNT_REQUEST_CODE
+intent.putExtra(EXTRA_PROVISIONING_MODE, PROVISIONING_MODE_MANAGED_PROFILE);%0A                finishWithIntent(intent
 );%0A 

remove login screen from admin-integrated flow