|
| 1 | +package jp.studyplus.android.sdk.example; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.v7.app.ActionBarActivity; |
| 8 | +import android.util.Log; |
| 9 | +import android.view.Menu; |
| 10 | +import android.view.MenuItem; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import com.google.common.eventbus.Subscribe; |
| 15 | + |
| 16 | +import jp.studyplus.android.sdk.service.api.ApiRequest; |
| 17 | +import jp.studyplus.android.sdk.service.api.ApiRequestListener; |
| 18 | +import jp.studyplus.android.sdk.service.api.StudyplusApi; |
| 19 | +import jp.studyplus.android.sdk.service.api.response.ErrorResponse; |
| 20 | +import jp.studyplus.android.sdk.service.api.response.SuccessfulResponse; |
| 21 | +import jp.studyplus.android.sdk.service.auth.AuthTransit; |
| 22 | +import jp.studyplus.android.sdk.service.auth.CertificationStore; |
| 23 | +import jp.studyplus.android.sdk.service.studyrecord.StudyRecordBuilder; |
| 24 | +import jp.studyplus.android.sdk.service.studyrecord.StudyRecordPostRequest; |
| 25 | + |
| 26 | +import static android.view.View.OnClickListener; |
| 27 | + |
| 28 | +public class ExampleActivity extends ActionBarActivity { |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setContentView(R.layout.activity_example); |
| 34 | + |
| 35 | + findViewById(R.id.start_auth) |
| 36 | + .setOnClickListener(new OnClickToAuth(this)); |
| 37 | + |
| 38 | + findViewById(R.id.post_study_record) |
| 39 | + .setOnClickListener(new OnClickToPost(this)); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 44 | + CertificationStore.create(this).update(data); |
| 45 | + } |
| 46 | + |
| 47 | + private static class OnClickToPost implements OnClickListener, ApiRequestListener { |
| 48 | + private final Context context; |
| 49 | + |
| 50 | + private OnClickToPost(Context context) { |
| 51 | + this.context = context; |
| 52 | + } |
| 53 | + @Override |
| 54 | + public void onClick(View v) { |
| 55 | + ApiRequest request = StudyRecordPostRequest.of( |
| 56 | + new StudyRecordBuilder() |
| 57 | + .setComment("勉強した!!!") |
| 58 | + .setAmountTotal(30) |
| 59 | + .setDurationSeconds(2 * 60) |
| 60 | + .build()); |
| 61 | + |
| 62 | + StudyplusApi.getClient(context).send(request.with(this)); |
| 63 | + } |
| 64 | + |
| 65 | + @Subscribe |
| 66 | + public void showMessage(SuccessfulResponse response){ |
| 67 | + Toast.makeText(context, "投稿完了!", Toast.LENGTH_SHORT).show(); |
| 68 | + } |
| 69 | + @Subscribe |
| 70 | + public void showError(ErrorResponse response){ |
| 71 | + Log.e(OnClickToPost.class.getName(), "response:" + response.getStatusCode()); |
| 72 | + } |
| 73 | + @Subscribe |
| 74 | + public void showException(Exception e){ |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static class OnClickToAuth implements OnClickListener { |
| 80 | + private final Activity activity; |
| 81 | + private final Context context; |
| 82 | + |
| 83 | + private OnClickToAuth(Activity activity) { |
| 84 | + this.activity = activity; |
| 85 | + this.context = activity.getApplicationContext(); |
| 86 | + } |
| 87 | + @Override |
| 88 | + public void onClick(View v) { |
| 89 | + AuthTransit.from(activity).startActivity( |
| 90 | + context.getString(R.string.sample_consumer_key), |
| 91 | + context.getString(R.string.sample_consumer_key_secret) |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 98 | + |
| 99 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 100 | + getMenuInflater().inflate(R.menu.example, menu); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 106 | + // Handle action bar item clicks here. The action bar will |
| 107 | + // automatically handle clicks on the Home/Up button, so long |
| 108 | + // as you specify a parent activity in AndroidManifest.xml. |
| 109 | + int id = item.getItemId(); |
| 110 | + if (id == R.id.action_settings) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + return super.onOptionsItemSelected(item); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments