github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/app/work-cross/GoNativeActivity.java (about)

     1  package org.golang.app;
     2  
     3  import android.app.Notification;
     4  import android.app.NotificationManager;
     5  import android.content.pm.ActivityInfo;
     6  import android.app.NativeActivity;
     7  import android.content.pm.PackageManager;
     8  import android.os.Bundle;
     9  import android.util.Log;
    10  import android.content.Intent;
    11  import android.net.Uri;
    12  import android.app.PendingIntent;
    13  import android.content.Context;
    14  import android.widget.Toast;
    15  import android.app.TaskStackBuilder;
    16  import android.support.v4.app.NotificationCompat;
    17  import android.view.KeyCharacterMap;
    18  import org.chromium.base.library_loader.LibraryLoader;
    19  import org.xwalk.core.XWalkView;
    20  import android.widget.LinearLayout;
    21  
    22  public class GoNativeActivity extends NativeActivity {
    23  
    24      private static GoNativeActivity goNativeActivity;
    25  
    26  	private LinearLayout commentsLayout;
    27  	private XWalkView mXWalkView;
    28  
    29  
    30      public GoNativeActivity() {
    31          super();
    32  		Log.d("Go", "GoNativeActivity");
    33          goNativeActivity = this;
    34      }  
    35          
    36      public void openBrowser(String url) {
    37      	  	Intent intent = new Intent(Intent.ACTION_VIEW);
    38  			Uri data = Uri.parse("http://localhost:8089");
    39  		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    40  			  intent.setData(data);
    41  			  startActivity(intent);
    42      }
    43      
    44      public void notif(String title, String text) {
    45  
    46  	  Log.d("Go", "GoNativeActivity notif");
    47  
    48    	  Intent intent = new Intent("org.golang.app.MainActivity");	    
    49  
    50  	  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    51  	  mBuilder.setSmallIcon(R.drawable.icon);
    52  	  mBuilder.setContentTitle(title);
    53  	  mBuilder.setContentText(text);
    54  	          
    55  	  Intent resultIntent = new Intent(this, MainActivity.class);
    56  	  TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    57  	  stackBuilder.addParentStack(MainActivity.class);
    58  
    59  	  // Adds the Intent that starts the Activity to the top of the stack
    60  	  stackBuilder.addNextIntent(resultIntent);
    61  	  PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    62  	  mBuilder.setContentIntent(resultPendingIntent);
    63  
    64  	  NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    65      
    66  	  // notificationID allows you to update the notification later on.
    67  	  mNotificationManager.notify(2, mBuilder.build());
    68  
    69      }    
    70  
    71      String getTmpdir() {
    72          return getCacheDir().getAbsolutePath();
    73      }
    74  
    75      String getFilesdir() {
    76  		return getExternalFilesDir(null).getAbsolutePath();
    77      }
    78  
    79  	int getRune(int deviceId, int keyCode, int metaState) {
    80  		try {
    81  			int rune = KeyCharacterMap.load(deviceId).get(keyCode, metaState);
    82  			if (rune == 0) {
    83  				return -1;
    84  			}
    85  			return rune;
    86  		} catch (KeyCharacterMap.UnavailableException e) {
    87  			return -1;
    88  		} catch (Exception e) {
    89  			Log.e("Go", "exception reading KeyCharacterMap", e);
    90  			return -1;
    91  		}
    92  	}
    93  
    94      public static void load() {
    95  
    96  		Log.d("Go", "GoNativeActivity load 00001");
    97          // Interestingly, NativeActivity uses a different method
    98          // to find native code to execute, avoiding
    99          // System.loadLibrary. The result is Java methods
   100          // implemented in C with JNIEXPORT (and JNI_OnLoad) are not
   101          // available unless an explicit call to System.loadLibrary
   102          // is done. So we do it here, borrowing the name of the
   103          // library from the same AndroidManifest.xml metadata used
   104          // by NativeActivity.
   105  		try {
   106  			System.loadLibrary("dcoin");
   107  
   108  		} catch (Exception e) {
   109  			Log.e("Go", "loadLibrary failed", e);
   110  		}
   111  
   112  
   113  
   114  	}
   115  
   116      public void onStart(Bundle savedInstanceState) {
   117  
   118  		Log.d("Go", "GoNativeActivity onStart");
   119  		try {
   120  			Intent intent = new Intent(Intent.ACTION_VIEW);
   121  			Uri data = Uri.parse("http://localhost:8089");
   122  			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   123  			intent.setData(data);
   124  			startActivity(intent);
   125  		} catch (Exception e) {
   126  			  Log.e("Go", "http://localhost:8089 failed", e);
   127  		 }
   128  		/*Runnable r = new Runnable() {
   129  			public void run() {
   130  				if (MyService.DcoinStarted(8089)) {
   131  					try {
   132  						Intent intent1 = new Intent(Intent.ACTION_VIEW);
   133  						Uri data = Uri.parse("http://localhost:8089");
   134  						intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   135  						intent1.setData(data);
   136  						startActivity(intent1);
   137  					} catch (Exception e) {
   138  						Log.e("Go", "http://localhost:8089 failed", e);
   139  					}
   140  				}
   141  			}
   142  		};
   143  		Thread t = new Thread(r);
   144  		t.start();*/
   145      }
   146  
   147      @Override
   148      public void onCreate(Bundle savedInstanceState) {
   149  
   150  		Log.d("Go", "GoNativeActivity onCreate");
   151  
   152  		/*try {
   153  			System.load("xwalkcore");
   154  		} catch (UnsatisfiedLinkError e) {
   155  			System.err.println("xwalkcore code library failed to load 0.\n" + e);
   156  			System.exit(1);
   157  		}
   158  		try {
   159  			System.load("xwalkdummy");
   160  		} catch (UnsatisfiedLinkError e) {
   161  			System.err.println("xwalkdummy code library failed to load 1.\n" + e);
   162  			System.exit(1);
   163  		}
   164  
   165  		setContentView(R.layout.activity_xwalk_embed_lib);
   166  		commentsLayout=(LinearLayout)findViewById(R.id.principal);
   167  		mXWalkView = new XWalkView(this, this);
   168  	  final String html = "<html><body bgcolor=\"#090998\"><h1>Hello world!</h1></body></html>";
   169              mXWalkView.load(null, html);
   170              commentsLayout.addView(mXWalkView);
   171  
   172  		//moveTaskToBack(true);*/
   173  
   174          super.onCreate(savedInstanceState);        
   175      }
   176  }