Created
June 3, 2013 06:23
-
-
Save hnakagawa/5696389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package triaina.sample; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.os.Parcel; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.app.NavUtils; | |
import android.support.v4.view.ViewPager; | |
import android.view.Gravity; | |
import android.view.KeyEvent; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import triaina.injector.activity.TriainaFragmentActivity; | |
import triaina.sample.R; | |
import triaina.webview.AbstractWebViewBridgeFragment; | |
import triaina.webview.Callback; | |
import triaina.webview.annotation.Bridge; | |
import triaina.webview.annotation.Domain; | |
import triaina.webview.annotation.Layout; | |
import triaina.webview.annotation.WebViewBridgeResouce; | |
import triaina.webview.entity.Params; | |
import triaina.webview.entity.Result; | |
public class MainActivity extends TriainaFragmentActivity { | |
public static final String HOST = "10.63.245.16"; | |
public static final int PORT = 3000; | |
/** | |
* The {@link android.support.v4.view.PagerAdapter} that will provide | |
* fragments for each of the sections. We use a | |
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which | |
* will keep every loaded fragment in memory. If this becomes too memory | |
* intensive, it may be best to switch to a | |
* {@link android.support.v4.app.FragmentStatePagerAdapter}. | |
*/ | |
SectionsPagerAdapter mSectionsPagerAdapter; | |
/** | |
* The {@link ViewPager} that will host the section contents. | |
*/ | |
ViewPager mViewPager; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
this.setContentView(R.layout.activity_main); | |
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); | |
// Set up the ViewPager with the sections adapter. | |
mViewPager = (ViewPager) findViewById(R.id.pager); | |
mViewPager.setAdapter(mSectionsPagerAdapter); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
NavUtils.navigateUpFromSameTask(this); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
return super.onKeyDown(keyCode, event); | |
} | |
/** | |
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to | |
* one of the primary sections of the app. | |
*/ | |
public class SectionsPagerAdapter extends FragmentPagerAdapter { | |
public SectionsPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int i) { | |
Fragment fragment = new WebViewFragment(i); | |
Bundle args = new Bundle(); | |
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public int getCount() { | |
return 3; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
switch (position) { | |
case 0: | |
return getString(R.string.title_section1).toUpperCase(); | |
case 1: | |
return getString(R.string.title_section2).toUpperCase(); | |
case 2: | |
return getString(R.string.title_section3).toUpperCase(); | |
} | |
return null; | |
} | |
} | |
@Domain(HOST) | |
@Layout(R.layout.fragment_webview) | |
@WebViewBridgeResouce(R.id.webview) | |
public static class WebViewFragment extends AbstractWebViewBridgeFragment { | |
private static final String[] ADDR_TABLE = new String[] { "reminder_message.html", "sensor", "network" }; | |
// private static final String[] ADDR_TABLE = new String[] { "basic", "sensor", "network" }; | |
private int mPosition; | |
private Handler mHandler; | |
public WebViewFragment(int position) { | |
mPosition = position; | |
mHandler = new Handler(Looper.getMainLooper()); | |
} | |
@Bridge("com.example.reminder_message.put") | |
public void postReminderMessage(final ReminderMessage msg, Callback<ReminderMessageResult> callback) { | |
mHandler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
getWebViewBridge().call("com.example.reminder_message.notify", new ReminderMessage(msg.mMessage)); | |
} | |
}, msg.mDelaySeccond * 1000); | |
callback.succeed(getWebViewBridge(), new ReminderMessageResult("ok")); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
if (!isRestored()) | |
getWebViewBridge().loadUrl("http://" + HOST + ":" + PORT + "/" + ADDR_TABLE[mPosition]); | |
} | |
} | |
public static class ReminderMessage implements Params { | |
private String mMessage; | |
private Long mDelaySeccond = Long.valueOf(0); | |
public ReminderMessage() { | |
} | |
public ReminderMessage(String message) { | |
mMessage = message; | |
} | |
public ReminderMessage(Parcel src) { | |
mMessage = src.readString(); | |
mDelaySeccond = src.readLong(); | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(mMessage); | |
dest.writeLong(mDelaySeccond); | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
} | |
public static class ReminderMessageResult implements Result { | |
private String mStatus; | |
public ReminderMessageResult(String status) { | |
mStatus = status; | |
} | |
public ReminderMessageResult(Parcel src) { | |
mStatus = src.readString(); | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(mStatus); | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
} | |
public static class DummySectionFragment extends Fragment { | |
public DummySectionFragment() { | |
} | |
public static final String ARG_SECTION_NUMBER = "section_number"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
TextView textView = new TextView(getActivity()); | |
textView.setGravity(Gravity.CENTER); | |
Bundle args = getArguments(); | |
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); | |
return textView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment