Upgrade to Pro — share decks privately, control downloads, hide ads and more …

はてなにおけるモダンAndroidアプリ開発入門

cockscomb
December 03, 2014

 はてなにおけるモダンAndroidアプリ開発入門

@ Hatena Engineer Seminar #3

cockscomb

December 03, 2014
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. X

  2. 剢 抟 宏 加 ꆃ أفٔٝز 鎘歗 ِ٦ؠؽٔ ذ؍ذأز 鎘歗

    ِ٦ؠؽٔ ذ؍ذأز ؙٕٔ٦ز ِ٦ؠؽٔ ذ؍ذأز أفٔٝز ٖؽُ٦
  3. Fragment fragment =
 getSupportFragmentManager().findFragmentById(R.id.container);
 
 if (fragment == null) {


    
 fragment = SomeFragment.newInstance();
 getSupportFragmentManager().beginTransaction()
 .replace(R.id.container, fragment)
 .commit();
 
 }
  4. public void testGetLineRanges() throws Exception {
 String text = "ABCDEFG\n"

    +
 "HIJKLMN\n" +
 "OPQRSTU\r\n" +
 "VWXYZ";
 
 {
 List<BaseCommand.Range> lineRanges =
 BaseCommand.getLineRanges(text, new BaseCommand.Range(8, 9));
 
 assertContentsInOrder(lineRanges, new BaseCommand.Range(8, 16));
 assertEquals("HIJKLMN\n", text.subSequence(8, 16));
 }
 
 }
  5. public void testDecorateWrappingInline() throws Exception {
 String text = "ABCDEFG\n"

    +
 "HIJKLMN\n";
 ArrayList<String> decorators = Lists.newArrayList("<b>", "</b>");
 
 {
 Editable editable =
 Editable.Factory.getInstance().newEditable(text);
 
 BaseCommand.Range newRange = BaseCommand.decorateWrappingInline(
 editable,
 new BaseCommand.Range(2, 4),
 decorators
 );
 
 assertEquals("AB<b>CD</b>EFG\n" +
 "HIJKLMN\n", editable.toString());
 assertEquals(new BaseCommand.Range(5, 7), newRange);
 }
 }
  6. AccountManager accountManager = AccountManager.get(this); // ΞΧ΢ϯτΛ௥Ճ Account account = new

    Account("cockscomb", "jp.ne.hatena"); accountManager.addAccountExplicitly(account, "Passw0rd!", null); // ΞΧ΢ϯτΛऔಘ Account[] accounts = accountManager.getAccountsByType("jp.ne.hatena");
  7. accountManager.getAuthToken( account, "jp.ne.hatena.token", true, new AccountManagerCallback<Bundle>() { @Override public void

    run(AccountManagerFuture<Bundle> future) { try { // τʔΫϯऔಘ Bundle result = future.getResult(); String authToken = result.getString( AccountManager.KEY_AUTHTOKEN ); } catch (OperationCanceledException | IOException | AuthenticatorException e) { // ΤϥʔϋϯυϦϯά } } }, null);
  8. public void getEntries(
 Response.Listener<JSONObject> listener,
 Response.ErrorListener errorListener
 ) {
 Request<JSONObject>

    request = new JsonObjectRequest(
 Request.Method.GET,
 HOST_HATENA + "/my/entries",
 listener,
 errorListener
 ) {
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 HashMap<String, String> headers = new HashMap<>();
 headers.put("X-Session-Key", mAuthenticator.getAuthToken());
 return headers;
 }
 };
 
 RequestQueue requestQueue = Volley.newRequestQueue(mContext);
 requestQueue.add(request);
 }
  9. public class BlogClient {
 private static final String API_URL =

    "https://blog.hatena.ne.jp";
 
 public interface BlogService {
 
 @GET("/my/entries")
 void getEntries(
 Callback<List<EntryResponse>> callback
 );
 
 @POST("/my/entries")
 void postEntry(
 @Body DeviceRequest entry,
 Callback<EntryResponse> callback
 );
 
 @Multipart
 @POST("/my/images")
 void uploadProfileIcon(
 @Part("image") TypedFile image,
 @Part("title") String title,
 Callback<ImageResponse> callback
 );
 
 }
 
 public static BlogService getSessionClient(Account account) {
 RestAdapter restAdapter = new RestAdapter.Builder()