2022-10-02 23:06:56 +02:00
package android.content ;
2023-06-22 11:45:46 +02:00
import android.app.ActivityManager ;
import android.app.AlarmManager ;
import android.app.Application ;
import android.app.KeyguardManager ;
import android.app.NotificationManager ;
import android.app.SharedPreferencesImpl ;
import android.content.BroadcastReceiver ;
import android.content.ClipboardManager ;
import android.content.Intent ;
import android.content.SharedPreferences ;
2022-10-26 18:39:04 +02:00
import android.content.pm.ApplicationInfo ;
2023-06-22 11:45:46 +02:00
import android.content.pm.PackageManager ;
2022-10-02 23:06:56 +02:00
import android.content.res.AssetManager ;
import android.content.res.Configuration ;
import android.content.res.Resources ;
import android.content.res.TypedArray ;
2023-06-22 11:45:46 +02:00
import android.hardware.SensorManager ;
import android.hardware.display.DisplayManager ;
import android.hardware.usb.UsbManager ;
import android.media.AudioManager ;
import android.media.MediaRouter ;
import android.net.ConnectivityManager ;
2022-10-02 23:06:56 +02:00
import android.os.Looper ;
2022-11-24 16:05:38 +01:00
import android.os.PowerManager ;
2023-06-22 11:45:46 +02:00
import android.os.Vibrator ;
import android.telephony.TelephonyManager ;
import android.util.AttributeSet ;
import android.util.DisplayMetrics ;
import android.util.Log ;
2022-10-02 23:06:56 +02:00
import android.view.WindowManager ;
import android.view.WindowManagerImpl ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
public class Context extends Object {
2022-11-04 19:21:45 +01:00
private final static String TAG = " Context " ;
2022-10-02 23:06:56 +02:00
2022-12-27 17:21:21 +01:00
public static final int MODE_PRIVATE = 0 ;
public static final String LOCATION_SERVICE = " location " ;
public static final String AUDIO_SERVICE = " audio " ;
public static final String DISPLAY_SERVICE = " display " ;
public static final String MEDIA_ROUTER_SERVICE = " media_router " ;
2022-10-02 23:06:56 +02:00
static AssetManager assets ;
static DisplayMetrics dm ;
static Configuration config ;
static Resources r ;
2022-11-24 23:10:27 +01:00
static ApplicationInfo application_info ;
2022-10-02 23:06:56 +02:00
2022-11-24 16:05:38 +01:00
static String apk_path = " /tmp/APK_PATH_SHOULD_HAVE_BEEN_FILLED_IN_BY_CODE_IN_main.c/ " ;
2022-10-02 23:06:56 +02:00
public /*← FIXME?*/ static Application this_application ;
File data_dir = null ;
File prefs_dir = null ;
File files_dir = null ;
2022-11-04 19:21:45 +01:00
File obb_dir = null ;
2022-10-02 23:06:56 +02:00
File cache_dir = null ;
static {
assets = new AssetManager ( ) ;
dm = new DisplayMetrics ( ) ;
config = new Configuration ( ) ;
r = new Resources ( assets , dm , config ) ;
this_application = new Application ( ) ; // TODO: the application context is presumably not identical to the Activity context, what is the difference for us though?
2022-11-24 23:10:27 +01:00
application_info = new ApplicationInfo ( ) ;
2022-10-02 23:06:56 +02:00
}
public Context ( ) {
System . out . println ( " new Context! this one is: " + this ) ;
}
2023-06-22 11:45:46 +02:00
public int checkPermission ( String permission , int pid , int uid ) {
2022-12-27 17:21:21 +01:00
return getPackageManager ( ) . checkPermission ( permission , " dummy " ) ;
}
2022-11-24 23:10:27 +01:00
public Resources . Theme getTheme ( ) {
return r . newTheme ( ) ;
}
2023-06-22 11:45:46 +02:00
public ApplicationInfo getApplicationInfo ( ) {
2022-11-24 23:10:27 +01:00
// TODO: do this somewhere saner?
application_info . nativeLibraryDir = ( new File ( getDataDirFile ( ) , " lib " ) ) . getAbsolutePath ( ) ;
return application_info ;
2022-10-26 18:39:04 +02:00
}
2022-10-02 23:06:56 +02:00
public Context getApplicationContext ( ) {
return ( Context ) this_application ;
}
public ContentResolver getContentResolver ( ) {
return new ContentResolver ( ) ;
}
public Object getSystemService ( String name ) {
switch ( name ) {
case " window " :
return new WindowManagerImpl ( ) ;
case " clipboard " :
return new ClipboardManager ( ) ;
case " sensor " :
return new SensorManager ( ) ;
case " connectivity " :
return new ConnectivityManager ( ) ;
case " keyguard " :
return new KeyguardManager ( ) ;
case " phone " :
return new TelephonyManager ( ) ;
case " audio " :
return new AudioManager ( ) ;
case " activity " :
return new ActivityManager ( ) ;
2022-10-26 18:39:04 +02:00
case " usb " :
return new UsbManager ( ) ;
case " vibrator " :
return new Vibrator ( ) ;
2022-11-24 16:05:38 +01:00
case " power " :
return new PowerManager ( ) ;
2022-12-27 17:21:21 +01:00
case " display " :
return new DisplayManager ( ) ;
2022-12-31 16:59:15 +01:00
case " media_router " :
return new MediaRouter ( ) ;
2023-01-14 14:32:37 +01:00
case " notification " :
return new NotificationManager ( ) ;
case " alarm " :
return new AlarmManager ( ) ;
2022-10-02 23:06:56 +02:00
default :
2023-06-22 11:45:46 +02:00
System . out . println ( " !!!!!!! getSystemService: case > " + name + " < is not implemented yet " ) ;
2022-10-02 23:06:56 +02:00
return null ;
}
}
2022-10-26 18:39:04 +02:00
public Intent registerReceiver ( BroadcastReceiver receiver , IntentFilter filter ) {
return new Intent ( ) ;
}
2022-11-04 19:21:45 +01:00
public Looper getMainLooper ( ) {
2022-10-02 23:06:56 +02:00
System . out . println ( " returning the main Looper, most definitely doing just that! " ) ;
return new Looper ( ) ;
}
public String getPackageName ( ) {
return " com.example.demo_app " ;
}
2022-11-24 16:05:38 +01:00
public String getPackageCodePath ( ) {
return apk_path ;
}
2022-10-02 23:06:56 +02:00
public final String getString ( int resId ) {
return r . getString ( resId ) ;
}
public PackageManager getPackageManager ( ) {
return new PackageManager ( ) ;
}
public Resources getResources ( ) {
return r ;
}
public AssetManager getAssets ( ) {
return assets ;
}
2022-11-04 19:21:45 +01:00
private File makeFilename ( File base , String name ) {
if ( name . indexOf ( File . separatorChar ) < 0 ) {
return new File ( base , name ) ;
}
throw new IllegalArgumentException (
2023-06-22 11:45:46 +02:00
" File " + name + " contains a path separator " ) ;
2022-11-04 19:21:45 +01:00
}
2022-10-02 23:06:56 +02:00
2022-11-04 19:21:45 +01:00
private File getDataDirFile ( ) {
2023-06-22 11:45:46 +02:00
if ( data_dir = = null ) {
2022-10-12 17:23:19 +02:00
data_dir = android . os . Environment . getExternalStorageDirectory ( ) ;
2022-10-02 23:06:56 +02:00
}
return data_dir ;
2022-11-04 19:21:45 +01:00
}
public File getFilesDir ( ) {
if ( files_dir = = null ) {
files_dir = new File ( getDataDirFile ( ) , " files " ) ;
}
if ( ! files_dir . exists ( ) ) {
2023-06-22 11:45:46 +02:00
if ( ! files_dir . mkdirs ( ) ) {
2022-11-04 19:21:45 +01:00
if ( files_dir . exists ( ) ) {
// spurious failure; probably racing with another process for this app
return files_dir ;
}
Log . w ( TAG , " Unable to create files directory " + files_dir . getPath ( ) ) ;
return null ;
}
}
return files_dir ;
}
public File getExternalFilesDir ( String type ) {
return getFilesDir ( ) ;
}
public File getObbDir ( ) {
2023-06-22 11:45:46 +02:00
if ( obb_dir = = null ) {
2022-11-04 19:21:45 +01:00
obb_dir = new File ( getDataDirFile ( ) , " obb " ) ;
}
return obb_dir ;
}
2022-10-02 23:06:56 +02:00
2022-12-27 17:21:21 +01:00
public File [ ] getObbDirs ( ) {
2023-06-22 11:45:46 +02:00
return new File [ ] { getObbDir ( ) } ;
2022-12-27 17:21:21 +01:00
}
2022-10-02 23:06:56 +02:00
// FIXME: should be something like /tmp/cache, but may need to create that directory
public File getCacheDir ( ) {
2023-06-22 11:45:46 +02:00
if ( cache_dir = = null ) {
cache_dir = new File ( " /tmp/ " ) ;
}
return cache_dir ;
2022-10-02 23:06:56 +02:00
}
2022-11-04 19:21:45 +01:00
private File getPreferencesDir ( ) {
2023-06-22 11:45:46 +02:00
if ( prefs_dir = = null ) {
prefs_dir = new File ( getDataDirFile ( ) , " shared_prefs " ) ;
}
return prefs_dir ;
2022-11-04 19:21:45 +01:00
}
2022-10-02 23:06:56 +02:00
2022-11-04 19:21:45 +01:00
public File getSharedPrefsFile ( String name ) {
return makeFilename ( getPreferencesDir ( ) , name + " .xml " ) ;
}
2022-10-02 23:06:56 +02:00
2022-11-04 19:21:45 +01:00
public SharedPreferences getSharedPreferences ( String name , int mode ) {
2023-06-22 11:45:46 +02:00
System . out . println ( " \ n \ n...> getSharedPreferences( " + name + " ,) \ n \ n " ) ;
2022-11-04 19:21:45 +01:00
File prefsFile = getSharedPrefsFile ( name ) ;
return new SharedPreferencesImpl ( prefsFile , mode ) ;
}
2022-10-02 23:06:56 +02:00
public ClassLoader getClassLoader ( ) {
// not perfect, but it's what we use for now as well, and it works
return ClassLoader . getSystemClassLoader ( ) ;
}
public ComponentName startService ( Intent service ) {
2023-06-22 11:45:46 +02:00
return new ComponentName ( " " , " " ) ;
2022-10-02 23:06:56 +02:00
}
// FIXME - it should be *trivial* to do actually implement this
public FileInputStream openFileInput ( String name ) {
return null ;
}
public FileOutputStream openFileOutput ( String name , int mode ) throws java . io . FileNotFoundException {
2023-06-22 11:45:46 +02:00
System . out . println ( " openFileOutput called for: ' " + name + " ' " ) ;
2022-10-12 17:23:19 +02:00
return new FileOutputStream ( android . os . Environment . getExternalStorageDirectory ( ) . getPath ( ) + " /files/ " + name ) ;
2022-10-02 23:06:56 +02:00
}
public int checkCallingOrSelfPermission ( String permission ) {
2023-06-22 11:45:46 +02:00
System . out . println ( " !!! app wants to know if it has a permission: > " + permission + " < " ) ;
2022-10-02 23:06:56 +02:00
return - 1 ; // PackageManager.PERMISSION_DENIED
}
2023-01-14 14:32:37 +01:00
public void registerComponentCallbacks ( ComponentCallbacks callbacks ) { }
2022-10-02 23:06:56 +02:00
// these may not look like typical stubs, but they definitely are stubs
2023-06-22 11:45:46 +02:00
public final TypedArray obtainStyledAttributes ( AttributeSet set , int [ ] attrs ) { return new TypedArray ( r , new int [ 1000 ] , new int [ 1000 ] , 0 ) ; }
public final TypedArray obtainStyledAttributes ( AttributeSet set , int [ ] attrs , int defStyleAttr , int defStyleRes ) { return new TypedArray ( r , new int [ 1000 ] , new int [ 1000 ] , 0 ) ; }
public final TypedArray obtainStyledAttributes ( int resid , int [ ] attrs ) { return new TypedArray ( r , new int [ 1000 ] , new int [ 1000 ] , 0 ) ; }
public final TypedArray obtainStyledAttributes ( int [ ] attrs ) { return new TypedArray ( r , new int [ 1000 ] , new int [ 1000 ] , 0 ) ; }
2022-10-02 23:06:56 +02:00
}