|
| 1 | +package com.microsoft.samples.onenotesample; |
| 2 | + |
| 3 | +import android.support.v7.app.ActionBarActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.util.Log; |
| 6 | +import android.view.Menu; |
| 7 | +import android.view.MenuItem; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.ArrayAdapter; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.ListView; |
| 12 | + |
| 13 | +import com.google.common.util.concurrent.FutureCallback; |
| 14 | +import com.google.common.util.concurrent.Futures; |
| 15 | +import com.microsoft.live.LiveAuthClient; |
| 16 | +import com.microsoft.services.odata.impl.LiveAuthDependencyResolver; |
| 17 | +import com.microsoft.onenote.api.Notebook; |
| 18 | +import com.microsoft.onenote.api.odata.OneNoteApiClient; |
| 19 | +import com.microsoft.services.odata.interfaces.LogLevel; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +public class MainActivity extends ActionBarActivity { |
| 26 | + |
| 27 | + private static final String TAG = "OneNoteSampleActivity"; |
| 28 | + |
| 29 | + private ListView lvNotebooks; |
| 30 | + private Button btnNotebooks; |
| 31 | + private OneNoteApiClient oneNoteClient; |
| 32 | + private LiveAuthDependencyResolver dependencyResolver; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void onCreate(Bundle savedInstanceState) { |
| 36 | + super.onCreate(savedInstanceState); |
| 37 | + setContentView(R.layout.activity_main); |
| 38 | + |
| 39 | + lvNotebooks = (ListView) findViewById(R.id.listView1); |
| 40 | + btnNotebooks = (Button) findViewById(R.id.button1); |
| 41 | + btnNotebooks.setOnClickListener(new View.OnClickListener() { |
| 42 | + @Override |
| 43 | + public void onClick(View v) { |
| 44 | + getNotebooks(); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + logon(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + protected OneNoteApiClient getOneNoteClient() { |
| 53 | + if (oneNoteClient == null) { |
| 54 | + oneNoteClient = new OneNoteApiClient(Constants.ONENOTE_API_ROOT, getDependencyResolver()); |
| 55 | + } |
| 56 | + return oneNoteClient; |
| 57 | + } |
| 58 | + |
| 59 | + protected LiveAuthDependencyResolver getDependencyResolver() { |
| 60 | + |
| 61 | + if (dependencyResolver == null) { |
| 62 | + LiveAuthClient theAuthClient = new LiveAuthClient(getApplicationContext(), Constants.CLIENT_ID, |
| 63 | + Arrays.asList(Constants.SCOPES)); |
| 64 | + |
| 65 | + dependencyResolver = new LiveAuthDependencyResolver(theAuthClient); |
| 66 | + |
| 67 | + dependencyResolver.getLogger().setEnabled(true); |
| 68 | + dependencyResolver.getLogger().setLogLevel(LogLevel.VERBOSE); |
| 69 | + } |
| 70 | + |
| 71 | + return dependencyResolver; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private void logon() { |
| 76 | + |
| 77 | + try { |
| 78 | + Futures.addCallback(this.getDependencyResolver().interactiveInitialize(this), new FutureCallback<Boolean>() { |
| 79 | + @Override |
| 80 | + public void onSuccess(Boolean result) { |
| 81 | + btnNotebooks.setEnabled(true); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onFailure(Throwable t) { |
| 86 | + getDependencyResolver().getLogger().log(t.getMessage(), LogLevel.ERROR); |
| 87 | + } |
| 88 | + }); |
| 89 | + } catch (Exception e) { |
| 90 | + Log.e(TAG, e.getMessage()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void getNotebooks() { |
| 95 | + |
| 96 | + Futures.addCallback(getOneNoteClient().getnotebooks().read(), new FutureCallback<List<Notebook>>() { |
| 97 | + @Override |
| 98 | + public void onSuccess(List<Notebook> notebooks) { |
| 99 | + List<String> nbNames = new ArrayList<String>(); |
| 100 | + for (Notebook nb : notebooks) { |
| 101 | + nbNames.add(nb.getname()); |
| 102 | + } |
| 103 | + final ArrayAdapter<String> adapter = |
| 104 | + new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1, nbNames); |
| 105 | + |
| 106 | + runOnUiThread(new Runnable() { |
| 107 | + @Override |
| 108 | + public void run() { |
| 109 | + lvNotebooks.setAdapter(adapter); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onFailure(Throwable t) { |
| 116 | + Log.e(TAG, t.getMessage()); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 126 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 127 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 133 | + // Handle action bar item clicks here. The action bar will |
| 134 | + // automatically handle clicks on the Home/Up button, so long |
| 135 | + // as you specify a parent activity in AndroidManifest.xml. |
| 136 | + int id = item.getItemId(); |
| 137 | + |
| 138 | + //noinspection SimplifiableIfStatement |
| 139 | + if (id == R.id.action_settings) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + if (id == R.id.action_logout) { |
| 144 | + getDependencyResolver().logout(); |
| 145 | + btnNotebooks.setEnabled(false); |
| 146 | + lvNotebooks.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, new String[0])); |
| 147 | + } |
| 148 | + |
| 149 | + if (id == R.id.action_login) { |
| 150 | + logon(); |
| 151 | + } |
| 152 | + |
| 153 | + return super.onOptionsItemSelected(item); |
| 154 | + } |
| 155 | +} |
0 commit comments