/* Copyright (c) Microsoft All Rights Reserved Apache 2.0 License 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. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. */ package com.microsoft.aad.taskapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.microsoft.aad.adal.AuthenticationCallback; import com.microsoft.aad.adal.AuthenticationContext; import com.microsoft.aad.adal.AuthenticationResult; import com.microsoft.aad.taskapplication.helpers.Constants; import com.microsoft.aad.taskapplication.helpers.InMemoryCacheStore; import com.microsoft.aad.taskapplication.helpers.TodoListHttpService; import com.microsoft.aad.taskapplication.helpers.WorkItemAdapter; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class ToDoActivity extends Activity { private final static String TAG = "ToDoActivity"; private AuthenticationContext mAuthContext; /** * Adapter to sync the items list with the view */ private WorkItemAdapter mAdapter = null; /** * Show this dialog when activity first launches to check if user has login * or not. */ private ProgressDialog mLoginProgressDialog; /** * Initializes the activity */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_todo_items); Toast.makeText(getApplicationContext(), TAG + "LifeCycle: OnCreate", Toast.LENGTH_SHORT) .show(); Button button = (Button) findViewById(R.id.switchUserButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ToDoActivity.this, UsersListActivity.class); startActivity(intent); } }); button = (Button) findViewById(R.id.addTaskButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ToDoActivity.this, AddTaskActivity.class); startActivity(intent); } }); button = (Button) findViewById(R.id.appSettingsButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ToDoActivity.this, SettingsActivity.class); startActivity(intent); } }); mLoginProgressDialog = new ProgressDialog(this); mLoginProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); mLoginProgressDialog.setMessage("Login in progress..."); mLoginProgressDialog.show(); // Ask for token and provide callback try { mAuthContext = new AuthenticationContext(ToDoActivity.this, Constants.AUTHORITY_URL, false, InMemoryCacheStore.getInstance()); mAuthContext.getCache().removeAll(); if(Constants.CORRELATION_ID != null && Constants.CORRELATION_ID.trim().length() !=0){ mAuthContext.setRequestCorrelationId(UUID.fromString(Constants.CORRELATION_ID)); } mAuthContext.acquireToken(ToDoActivity.this, Constants.RESOURCE_ID, Constants.CLIENT_ID, Constants.REDIRECT_URL, Constants.USER_HINT, "nux=1&" + Constants.EXTRA_QP, new AuthenticationCallback<AuthenticationResult>() { @Override public void onError(Exception exc) { if (mLoginProgressDialog.isShowing()) { mLoginProgressDialog.dismiss(); } SimpleAlertDialog.showAlertDialog(ToDoActivity.this, "Failed to get token", exc.getMessage()); } @Override public void onSuccess(AuthenticationResult result) { if (mLoginProgressDialog.isShowing()) { mLoginProgressDialog.dismiss(); } if (result != null && !result.getAccessToken().isEmpty()) { setLocalToken(result); updateLoggedInUser(); getTasks(); } else { //TODO: popup error alert } } }); } catch (Exception e) { SimpleAlertDialog.showAlertDialog(getApplicationContext(), "Exception caught", e.getMessage()); } Toast.makeText(getApplicationContext(), TAG + "done", Toast.LENGTH_SHORT).show(); } private void updateLoggedInUser() { TextView textView = (TextView) findViewById(R.id.userLoggedIn); textView.setText("N/A"); if (Constants.CURRENT_RESULT != null) { if (Constants.CURRENT_RESULT.getIdToken() != null) { textView.setText(Constants.CURRENT_RESULT.getUserInfo().getDisplayableId()); } else { textView.setText("User with No ID Token"); } } } private void getTasks() { if (Constants.CURRENT_RESULT == null || Constants.CURRENT_RESULT.getAccessToken().isEmpty()) return; List<String> items = new ArrayList<>(); try { items = new TodoListHttpService().getAllItems(Constants.CURRENT_RESULT.getAccessToken()); } catch (Exception e) { items = new ArrayList<>(); } ListView listview = (ListView) findViewById(R.id.listViewToDo); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items); listview.setAdapter(adapter); } private URL getEndpointUrl() { URL endpoint = null; try { endpoint = new URL(Constants.SERVICE_URL); } catch (MalformedURLException e) { e.printStackTrace(); } return endpoint; } private void initAppTables() { try { // Get the Mobile Service Table instance to use // mToDoTable = mClient.getTable(WorkItem.class); // mToDoTable.TABvLES_URL = "/api/"; //mTextNewToDo = (EditText)findViewById(R.id.listViewToDo); // Create an adapter to bind the items with the view //mAdapter = new WorkItemAdapter(ToDoActivity.this, R.layout.listViewToDo); ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo); listViewToDo.setAdapter(mAdapter); } catch (Exception e) { createAndShowDialog(new Exception( "There was an error creating the Mobile Service. Verify the URL"), "Error"); } } private void getToken(final AuthenticationCallback callback) { // one of the acquireToken overloads mAuthContext.acquireToken(ToDoActivity.this, Constants.RESOURCE_ID, Constants.CLIENT_ID, Constants.REDIRECT_URL, Constants.USER_HINT, "nux=1&" + Constants.EXTRA_QP, callback); } private AuthenticationResult getLocalToken() { return Constants.CURRENT_RESULT; } private void setLocalToken(AuthenticationResult newToken) { Constants.CURRENT_RESULT = newToken; } @Override public void onResume() { super.onResume(); // Always call the superclass method first updateLoggedInUser(); // User can click logout, it will come back here // It should refresh list again getTasks(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); mAuthContext.onActivityResult(requestCode, resultCode, data); } /** * Creates a dialog and shows it * * @param exception The exception to show in the dialog * @param title The dialog title */ private void createAndShowDialog(Exception exception, String title) { createAndShowDialog(exception.toString(), title); } /** * Creates a dialog and shows it * * @param message The dialog message * @param title The dialog title */ private void createAndShowDialog(String message, String title) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message); builder.setTitle(title); builder.create().show(); } }