Search in sources :

Example 26 with DexClassLoader

use of dalvik.system.DexClassLoader in project cw-omnibus by commonsguy.

the class ThingsLoaderThunk method loadThunk.

private void loadThunk() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    DexClassLoader dcl = new DexClassLoader(apkPath.getAbsolutePath(), cachePath.getAbsolutePath(), null, getClass().getClassLoader());
    Class<ThingsLoader> clazz = (Class<ThingsLoader>) dcl.loadClass(classname);
    extImpl = clazz.newInstance();
}
Also used : ThingsLoader(com.commonsware.android.dyncode.api.ThingsLoader) DexClassLoader(dalvik.system.DexClassLoader)

Example 27 with DexClassLoader

use of dalvik.system.DexClassLoader in project atlas by alibaba.

the class BundleArchiveRevision method findClass.

Class<?> findClass(String className, ClassLoader cl) throws ClassNotFoundException {
    try {
        Class<?> clazz = null;
        if (AtlasHacks.LexFile != null && AtlasHacks.LexFile.getmClass() != null) {
            if (dexClassLoader == null) {
                File libDir = new File(RuntimeVariables.androidApplication.getFilesDir().getParentFile(), "lib");
                dexClassLoader = new DexClassLoader(bundleFile.getAbsolutePath(), revisionDir.getAbsolutePath(), libDir.getAbsolutePath(), cl) {

                    @Override
                    public String findLibrary(String name) {
                        String path = super.findLibrary(name);
                        if (TextUtils.isEmpty(path)) {
                            String fileName = System.mapLibraryName(name);
                            File soFile = findSoLibrary(fileName);
                            if (soFile != null && soFile.exists()) {
                                return soFile.getAbsolutePath();
                            }
                            try {
                                return (String) AtlasHacks.ClassLoader_findLibrary.invoke(Framework.getSystemClassLoader(), name);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return null;
                        } else {
                            return path;
                        }
                    }
                };
            }
            clazz = (Class<?>) AtlasHacks.DexClassLoader_findClass.invoke(dexClassLoader, className);
            return clazz;
        }
        if (isDexOpted() == false) {
            optDexFile();
        }
        if (dexFile == null) {
            //                loadDex(new File(revisionDir, BUNDLE_ODEX_FILE));
            optDexFile();
        }
        clazz = dexFile.loadClass(className, cl);
        return clazz;
    } catch (IllegalArgumentException e) {
    } catch (InvocationTargetException e) {
    } catch (Exception e) {
        if (e instanceof ClassNotFoundException) {
        //do nothing
        } else if (e instanceof DexLoadException) {
            throw (DexLoadException) e;
        } else {
            Log.e("Framework", "Exception while find class in archive revision: " + bundleFile.getAbsolutePath(), e);
        }
    }
    return null;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) ZipFile(java.util.zip.ZipFile) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with DexClassLoader

use of dalvik.system.DexClassLoader in project atlas by alibaba.

the class BundleArchiveRevision method optDexFile.

public synchronized void optDexFile() {
    if (isDexOpted()) {
        return;
    }
    if (AtlasHacks.LexFile != null && AtlasHacks.LexFile.getmClass() != null) {
        //yunos
        // TODO: need also cover logic of filelocks for YunOS.
        new DexClassLoader(bundleFile.getAbsolutePath(), revisionDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
        isDexOptDone = true;
        return;
    }
    File odexFile = new File(revisionDir, BUNDLE_ODEX_FILE);
    long START = 0;
    START = System.currentTimeMillis();
    try {
        if (AtlasFileLock.getInstance().LockExclusive(odexFile) == false) {
            Log.e("Framework", "Failed to get file lock for " + bundleFile.getAbsolutePath());
        }
        if (dexFile == null) {
            dexFile = com.taobao.android.runtime.RuntimeUtils.loadDex(RuntimeVariables.androidApplication, bundleFile.getAbsolutePath(), odexFile.getAbsolutePath(), 0);
        //                dexFile = DexFile.loadDex(bundleFile.getAbsolutePath(), odexFile.getAbsolutePath(), 0);
        }
        //9月份版本明天发布先不集成
        //            isDexOptDone = checkDexValid(dexFile);
        isDexOptDone = true;
    } catch (IOException e) {
        AtlasMonitor.getInstance().trace(AtlasMonitor.DEXOPT_FAIL, location, AtlasMonitor.DEXOPT_FAIL_MSG, FileUtils.getDataAvailableSpace());
        if (odexFile.exists()) {
            odexFile.delete();
        }
        Log.e("Framework", "Failed optDexFile '" + bundleFile.getAbsolutePath() + "' >>> ", e);
    } finally {
        AtlasFileLock.getInstance().unLock(odexFile);
    }
    Log.e("Framework", "bundle archieve dexopt bundle " + bundleFile.getAbsolutePath() + " cost time = " + (System.currentTimeMillis() - START) + " ms");
    return;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) ZipFile(java.util.zip.ZipFile)

Example 29 with DexClassLoader

use of dalvik.system.DexClassLoader in project android_frameworks_base by ParanoidAndroid.

the class PhoneWindowManager method init.

/** {@inheritDoc} */
@Override
public void init(Context context, IWindowManager windowManager, WindowManagerFuncs windowManagerFuncs) {
    mContext = context;
    mWindowManager = windowManager;
    mWindowManagerFuncs = windowManagerFuncs;
    mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
    if (!mHeadless) {
        // don't create KeyguardViewMediator if headless
        mKeyguardMediator = new KeyguardViewMediator(context, null);
    }
    mHandler = new PolicyHandler();
    mOrientationListener = new MyOrientationListener(mContext, mHandler);
    try {
        mOrientationListener.setCurrentRotation(windowManager.getRotation());
    } catch (RemoteException ex) {
    }
    updateHybridLayout();
    mSettingsObserver = new SettingsObserver(mHandler);
    mSettingsObserver.observe();
    // SystemUI reboot
    mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.USER_INTERFACE_STATE), false, new ContentObserver(new Handler()) {

        @Override
        public void onChange(boolean selfChange) {
            // Return for reset triggers
            if (Settings.System.getInt(mContext.getContentResolver(), Settings.System.USER_INTERFACE_STATE, 0) == 0) {
                return;
            }
            // Update layout
            update(true);
            // Reset trigger
            Settings.System.putInt(mContext.getContentResolver(), Settings.System.USER_INTERFACE_STATE, 0);
        }
    });
    // Expanded desktop
    mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.EXPANDED_DESKTOP_STATE), false, new ContentObserver(new Handler()) {

        @Override
        public void onChange(boolean selfChange) {
            updateHybridLayout();
            update(false);
        }
    });
    mShortcutManager = new ShortcutManager(context, mHandler);
    mShortcutManager.observe();
    mUiMode = context.getResources().getInteger(com.android.internal.R.integer.config_defaultUiModeType);
    mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
    mHomeIntent.addCategory(Intent.CATEGORY_HOME);
    mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    mCarDockIntent = new Intent(Intent.ACTION_MAIN, null);
    mCarDockIntent.addCategory(Intent.CATEGORY_CAR_DOCK);
    mCarDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    mDeskDockIntent = new Intent(Intent.ACTION_MAIN, null);
    mDeskDockIntent.addCategory(Intent.CATEGORY_DESK_DOCK);
    mDeskDockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mBroadcastWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PhoneWindowManager.mBroadcastWakeLock");
    mEnableShiftMenuBugReports = "1".equals(SystemProperties.get("ro.debuggable"));
    mLidOpenRotation = readRotation(com.android.internal.R.integer.config_lidOpenRotation);
    mCarDockRotation = readRotation(com.android.internal.R.integer.config_carDockRotation);
    mDeskDockRotation = readRotation(com.android.internal.R.integer.config_deskDockRotation);
    mUndockedHdmiRotation = readRotation(com.android.internal.R.integer.config_undockedHdmiRotation);
    mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(com.android.internal.R.bool.config_carDockEnablesAccelerometer);
    mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(com.android.internal.R.bool.config_deskDockEnablesAccelerometer);
    mLidKeyboardAccessibility = mContext.getResources().getInteger(com.android.internal.R.integer.config_lidKeyboardAccessibility);
    mLidNavigationAccessibility = mContext.getResources().getInteger(com.android.internal.R.integer.config_lidNavigationAccessibility);
    mLidControlsSleep = mContext.getResources().getBoolean(com.android.internal.R.bool.config_lidControlsSleep);
    mHasRemovableLid = mContext.getResources().getBoolean(com.android.internal.R.bool.config_hasRemovableLid);
    mDeviceHardwareKeys = mContext.getResources().getInteger(com.android.internal.R.integer.config_deviceHardwareKeys);
    mHasHomeKey = ((mDeviceHardwareKeys & KEY_MASK_HOME) != 0);
    mHasMenuKey = ((mDeviceHardwareKeys & KEY_MASK_MENU) != 0);
    mHasAssistKey = ((mDeviceHardwareKeys & KEY_MASK_ASSIST) != 0);
    mHasAppSwitchKey = ((mDeviceHardwareKeys & KEY_MASK_APP_SWITCH) != 0);
    // register for dock events
    IntentFilter filter = new IntentFilter();
    filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
    filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);
    filter.addAction(UiModeManager.ACTION_ENTER_DESK_MODE);
    filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE);
    filter.addAction(Intent.ACTION_DOCK_EVENT);
    Intent intent = context.registerReceiver(mDockReceiver, filter);
    if (intent != null) {
        // Retrieve current sticky dock event broadcast.
        mDockMode = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, Intent.EXTRA_DOCK_STATE_UNDOCKED);
    }
    // register for dream-related broadcasts
    filter = new IntentFilter();
    filter.addAction(Intent.ACTION_DREAMING_STARTED);
    filter.addAction(Intent.ACTION_DREAMING_STOPPED);
    context.registerReceiver(mDreamReceiver, filter);
    // register for multiuser-relevant broadcasts
    filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
    context.registerReceiver(mMultiuserReceiver, filter);
    mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    mLongPressVibePattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_longPressVibePattern);
    mVirtualKeyVibePattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_virtualKeyVibePattern);
    mKeyboardTapVibePattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_keyboardTapVibePattern);
    mSafeModeDisabledVibePattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_safeModeDisabledVibePattern);
    mSafeModeEnabledVibePattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_safeModeEnabledVibePattern);
    mScreenshotChordEnabled = mContext.getResources().getBoolean(com.android.internal.R.bool.config_enableScreenshotChord);
    mGlobalKeyManager = new GlobalKeyManager(mContext);
    // Controls rotation and the like.
    initializeHdmiState();
    // Match current screen state.
    if (mPowerManager.isScreenOn()) {
        screenTurningOn(null);
    } else {
        screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
    }
    String deviceKeyHandlerLib = mContext.getResources().getString(com.android.internal.R.string.config_deviceKeyHandlerLib);
    String deviceKeyHandlerClass = mContext.getResources().getString(com.android.internal.R.string.config_deviceKeyHandlerClass);
    if (!deviceKeyHandlerLib.equals("") && !deviceKeyHandlerClass.equals("")) {
        DexClassLoader loader = new DexClassLoader(deviceKeyHandlerLib, new ContextWrapper(mContext).getCacheDir().getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
        try {
            Class<?> klass = loader.loadClass(deviceKeyHandlerClass);
            Constructor<?> constructor = klass.getConstructor(Context.class);
            mDeviceKeyHandler = (DeviceKeyHandler) constructor.newInstance(mContext);
            if (DEBUG)
                Slog.d(TAG, "Device key handler loaded");
        } catch (Exception e) {
            Slog.w(TAG, "Could not instantiate device key handler " + deviceKeyHandlerClass + " from class " + deviceKeyHandlerLib, e);
        }
    }
}
Also used : IntentFilter(android.content.IntentFilter) KeyguardViewMediator(com.android.internal.policy.impl.keyguard.KeyguardViewMediator) DexClassLoader(dalvik.system.DexClassLoader) Handler(android.os.Handler) DeviceKeyHandler(com.android.internal.os.DeviceKeyHandler) Intent(android.content.Intent) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) RemoteException(android.os.RemoteException) ContextWrapper(android.content.ContextWrapper) ContentObserver(android.database.ContentObserver)

Example 30 with DexClassLoader

use of dalvik.system.DexClassLoader in project freeline by alibaba.

the class NativeUtils method injectHackNativeLib.

public static void injectHackNativeLib(String libraryDir, PathClassLoader classLoader) {
    try {
        Log.i(TAG, "native lib inject process start...");
        String refFieldName = "nativeLibraryPathElements";
        if (Build.VERSION.SDK_INT < 23) {
            refFieldName = "nativeLibraryDirectories";
        }
        Object pathListObject = ReflectUtil.getField(classLoader, "pathList");
        Object nativeLibraryPathElementsObject = ReflectUtil.getField(pathListObject, refFieldName);
        Field nativeLibraryPathElementsFiled = ReflectUtil.fieldGetOrg(pathListObject, refFieldName);
        DexClassLoader dumbDexClassLoader = new DexClassLoader("", libraryDir, libraryDir, classLoader.getParent());
        Object dynamicNativeLibraryPathElements = ReflectUtil.getField(ReflectUtil.getField(dumbDexClassLoader, "pathList"), refFieldName);
        Object dynamicNativeLibraryPathElement = Array.get(dynamicNativeLibraryPathElements, 0);
        int lengthOfNewNativeLibraryPathElements = Array.getLength(nativeLibraryPathElementsObject) + 1;
        Object newNativeLibraryPathElements = Array.newInstance(nativeLibraryPathElementsFiled.getType().getComponentType(), lengthOfNewNativeLibraryPathElements);
        Array.set(newNativeLibraryPathElements, 0, dynamicNativeLibraryPathElement);
        for (int i = 1; i < lengthOfNewNativeLibraryPathElements; i++) {
            Object object = Array.get(nativeLibraryPathElementsObject, i - 1);
            Array.set(newNativeLibraryPathElements, i, object);
        }
        ReflectUtil.setField(pathListObject, refFieldName, newNativeLibraryPathElements);
        Log.i(TAG, "inject native lib success " + newNativeLibraryPathElements);
    } catch (Exception ex) {
        Log.e(TAG, "inject native lib failed", ex);
    }
}
Also used : Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader)

Aggregations

DexClassLoader (dalvik.system.DexClassLoader)51 File (java.io.File)27 PathClassLoader (dalvik.system.PathClassLoader)13 DexFile (dalvik.system.DexFile)7 IOException (java.io.IOException)7 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 Field (java.lang.reflect.Field)5 ZipFile (java.util.zip.ZipFile)5 TargetApi (android.annotation.TargetApi)3 Intent (android.content.Intent)3 Resources (android.content.res.Resources)3 RemoteException (android.os.RemoteException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Context (android.content.Context)2 ContextWrapper (android.content.ContextWrapper)2 AssetManager (android.content.res.AssetManager)2 Handler (android.os.Handler)2 TextView (android.widget.TextView)2