github.com/shranet/mobile@v0.0.0-20200814083559-5702cdcd481b/app/GoNativeActivity.java (about)

     1  package org.golang.app;
     2  
     3  import android.app.Activity;
     4  import android.content.DialogInterface;
     5  import android.app.NativeActivity;
     6  import android.app.AlertDialog;
     7  import android.content.Context;
     8  import android.content.pm.ActivityInfo;
     9  import android.content.pm.PackageManager;
    10  import android.os.Bundle;
    11  import android.util.Log;
    12  import android.view.KeyCharacterMap;
    13  import android.widget.Toast;
    14  import android.provider.Settings.Secure;
    15  import android.content.Intent;
    16  import android.os.SystemClock;
    17  
    18  public class GoNativeActivity extends NativeActivity {
    19  	private static GoNativeActivity goNativeActivity;
    20  
    21  	public GoNativeActivity() {
    22  		super();
    23  		goNativeActivity = this;
    24  	}
    25  
    26  	String getTmpdir() {
    27  		return getCacheDir().getAbsolutePath();
    28  	}
    29  
    30  	String getAssetsPath() {
    31  	    Log.d("Go", "GoNativeActivity getAssetsPath");
    32  		return getFilesDir().getAbsolutePath();
    33      }
    34  
    35      String getAndroidId() {
    36          Log.d("Go", "GoNativeActivity getAndroidId");
    37          return Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
    38      }
    39  
    40  	static int getRune(int deviceId, int keyCode, int metaState) {
    41  		try {
    42  			int rune = KeyCharacterMap.load(deviceId).get(keyCode, metaState);
    43  			if (rune == 0) {
    44  				return -1;
    45  			}
    46  			return rune;
    47  		} catch (KeyCharacterMap.UnavailableException e) {
    48  			return -1;
    49  		} catch (Exception e) {
    50  			Log.e("Go", "exception reading KeyCharacterMap", e);
    51  			return -1;
    52  		}
    53  	}
    54  
    55  	private void load() {
    56          // Interestingly, NativeActivity uses a different method
    57          // to find native code to execute, avoiding
    58          // System.loadLibrary. The result is Java methods
    59          // implemented in C with JNIEXPORT (and JNI_OnLoad) are not
    60          // available unless an explicit call to System.loadLibrary
    61          // is done. So we do it here, borrowing the name of the
    62          // library from the same AndroidManifest.xml metadata used
    63          // by NativeActivity.
    64          try {
    65              ActivityInfo ai = getPackageManager().getActivityInfo(
    66                      getIntent().getComponent(), PackageManager.GET_META_DATA);
    67              if (ai.metaData == null) {
    68                  Log.e("Go", "loadLibrary: no manifest metadata found");
    69                  return;
    70              }
    71              String libName = ai.metaData.getString("android.app.lib_name");
    72              System.loadLibrary(libName);
    73          } catch (Exception e) {
    74              Log.e("Go", "loadLibrary failed", e);
    75          }
    76      }
    77  
    78  	@Override
    79  	public void onCreate(Bundle savedInstanceState) {
    80          load();
    81  		super.onCreate(savedInstanceState);
    82  
    83  // 		Intent intent = new Intent("org.golang.app.MyService");
    84  //         this.startService(intent);
    85  
    86          new Thread(new Runnable() {
    87              public void run() {
    88                  SystemClock.sleep(3000);
    89  
    90                  final Intent dialogIntent = new Intent(getApplicationContext(), WViewActivity.class);
    91                  dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    92                  startActivity(dialogIntent);
    93              }
    94            }).start();
    95  	}
    96  }