Search in sources :

Example 81 with Application

use of android.app.Application in project platform_frameworks_base by android.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 82 with Application

use of android.app.Application in project freeline by alibaba.

the class MonkeyPatcher method monkeyPatchApplication.

// Lots of conversions with generic types
@SuppressWarnings("unchecked")
public static void monkeyPatchApplication(Context context, Application bootstrap, Application realApplication, String externalResourceFile) {
    // Application class.
    try {
        // Find the ActivityThread instance for the current thread
        Class<?> activityThread = Class.forName("android.app.ActivityThread");
        Object currentActivityThread = getActivityThread(context, activityThread);
        // Find the mInitialApplication field of the ActivityThread to the real application
        Field mInitialApplication = activityThread.getDeclaredField("mInitialApplication");
        mInitialApplication.setAccessible(true);
        Application initialApplication = (Application) mInitialApplication.get(currentActivityThread);
        if (realApplication != null && initialApplication == bootstrap) {
            mInitialApplication.set(currentActivityThread, realApplication);
        }
        // real one
        if (realApplication != null) {
            Field mAllApplications = activityThread.getDeclaredField("mAllApplications");
            mAllApplications.setAccessible(true);
            List<Application> allApplications = (List<Application>) mAllApplications.get(currentActivityThread);
            for (int i = 0; i < allApplications.size(); i++) {
                if (allApplications.get(i) == bootstrap) {
                    allApplications.set(i, realApplication);
                }
            }
        }
        // Figure out how loaded APKs are stored.
        // API version 8 has PackageInfo, 10 has LoadedApk. 9, I don't know.
        Class<?> loadedApkClass;
        try {
            loadedApkClass = Class.forName("android.app.LoadedApk");
        } catch (ClassNotFoundException e) {
            loadedApkClass = Class.forName("android.app.ActivityThread$PackageInfo");
        }
        Field mApplication = loadedApkClass.getDeclaredField("mApplication");
        mApplication.setAccessible(true);
        Field mResDir = loadedApkClass.getDeclaredField("mResDir");
        mResDir.setAccessible(true);
        // 10 doesn't have this field, 14 does. Fortunately, there are not many Honeycomb devices
        // floating around.
        Field mLoadedApk = null;
        try {
            mLoadedApk = Application.class.getDeclaredField("mLoadedApk");
        } catch (NoSuchFieldException e) {
        // According to testing, it's okay to ignore this.
        }
        //   - Set Application#mLoadedApk to the found LoadedApk instance
        for (String fieldName : new String[] { "mPackages", "mResourcePackages" }) {
            Field field = activityThread.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object value = field.get(currentActivityThread);
            for (Map.Entry<String, WeakReference<?>> entry : ((Map<String, WeakReference<?>>) value).entrySet()) {
                Object loadedApk = entry.getValue().get();
                if (loadedApk == null) {
                    continue;
                }
                if (mApplication.get(loadedApk) == bootstrap) {
                    if (realApplication != null) {
                        mApplication.set(loadedApk, realApplication);
                    }
                    if (externalResourceFile != null) {
                        mResDir.set(loadedApk, externalResourceFile);
                    }
                    if (realApplication != null && mLoadedApk != null) {
                        mLoadedApk.set(realApplication, loadedApk);
                    }
                }
            }
        }
    } catch (Throwable e) {
        throw new IllegalStateException(e);
    }
}
Also used : Field(java.lang.reflect.Field) WeakReference(java.lang.ref.WeakReference) List(java.util.List) Application(android.app.Application) HashMap(java.util.HashMap) Map(java.util.Map) ArrayMap(android.util.ArrayMap)

Example 83 with Application

use of android.app.Application in project DroidPlugin by DroidPluginTeam.

the class INotificationManagerHookHandle method hackRemoteViews.

private void hackRemoteViews(RemoteViews remoteViews) throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
    if (remoteViews != null && !TextUtils.equals(remoteViews.getPackage(), mHostContext.getPackageName())) {
        if (sSystemLayoutResIds.containsKey(remoteViews.getLayoutId())) {
            Object mActionsObj = FieldUtils.readField(remoteViews, "mActions");
            if (mActionsObj instanceof Collection) {
                Collection mActions = (Collection) mActionsObj;
                String aPackage = remoteViews.getPackage();
                Application pluginContent = PluginProcessManager.getPluginContext(aPackage);
                if (pluginContent != null) {
                    Iterator iterable = mActions.iterator();
                    Class TextViewDrawableActionClass = null;
                    try {
                        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
                            TextViewDrawableActionClass = Class.forName(RemoteViews.class.getName() + "$TextViewDrawableAction");
                        }
                    } catch (ClassNotFoundException e) {
                    }
                    Class ReflectionActionClass = Class.forName(RemoteViews.class.getName() + "$ReflectionAction");
                    while (iterable.hasNext()) {
                        Object action = iterable.next();
                        if (ReflectionActionClass.isInstance(action)) {
                            //???这里这样是对的么?
                            String methodName = (String) FieldUtils.readField(action, "methodName");
                            //String methodName;,int type; Object value;
                            if ("setImageResource".equals(methodName)) {
                                //setInt(viewId, "setImageResource", srcId);
                                Object BITMAP = FieldUtils.readStaticField(action.getClass(), "BITMAP");
                                int resId = (Integer) FieldUtils.readField(action, "value");
                                Bitmap bitmap = BitmapFactory.decodeResource(pluginContent.getResources(), resId);
                                FieldUtils.writeField(action, "type", BITMAP);
                                FieldUtils.writeField(action, "value", bitmap);
                                FieldUtils.writeField(action, "methodName", "setImageBitmap");
                            } else if ("setImageURI".equals(methodName)) {
                                //setUri(viewId, "setImageURI", uri);
                                //TODO RemoteViews.setImageURI 其实应该适配的。
                                iterable.remove();
                            } else if ("setLabelFor".equals(methodName)) {
                                //TODO RemoteViews.setLabelFor 其实应该适配的。
                                iterable.remove();
                            }
                        } else if (TextViewDrawableActionClass != null && TextViewDrawableActionClass.isInstance(action)) {
                            iterable.remove();
                        //                                if ("setTextViewCompoundDrawables".equals(methodName)) {
                        //                                    iterable.remove();   //TODO RemoteViews.setTextViewCompoundDrawables 其实应该适配的。
                        //                                } else if ("setTextViewCompoundDrawablesRelative".equals(methodName)) {
                        //                                    iterable.remove();   //TODO RemoteViews.setTextViewCompoundDrawablesRelative 其实应该适配的。
                        //                                }
                        }
                    }
                }
            }
        }
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            FieldUtils.writeField(remoteViews, "mApplication", mHostContext.getApplicationInfo());
        } else {
            FieldUtils.writeField(remoteViews, "mPackage", mHostContext.getPackageName());
        }
    }
}
Also used : RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) Iterator(java.util.Iterator) Collection(java.util.Collection) Application(android.app.Application)

Example 84 with Application

use of android.app.Application in project mosby by sockeqwe.

the class PresenterManagerTest method getPresenterThrowsNullPointerException.

@Test
public void getPresenterThrowsNullPointerException() {
    try {
        PresenterManager.getPresenter(null, "123");
        Assert.fail("Exception expected");
    } catch (NullPointerException e) {
    }
    try {
        Activity activity = Mockito.mock(Activity.class);
        Application application = Mockito.mock(Application.class);
        Mockito.when(activity.getApplication()).thenReturn(application);
        PresenterManager.getPresenter(activity, null);
        Assert.fail("Exception expected");
    } catch (NullPointerException e) {
    }
}
Also used : Activity(android.app.Activity) Application(android.app.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 85 with Application

use of android.app.Application in project mosby by sockeqwe.

the class PresenterManagerTest method removesLifecycleListener.

@Test
public void removesLifecycleListener() {
    Activity activity = Mockito.mock(Activity.class);
    Application application = Mockito.mock(Application.class);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(false);
    // This one also registers for
    ActivityScopedCache scopedCache = PresenterManager.getOrCreateActivityScopedCache(activity);
    Mockito.verify(application, Mockito.times(1)).registerActivityLifecycleCallbacks(PresenterManager.activityLifecycleCallbacks);
    PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(activity);
    Mockito.verify(application, Mockito.times(1)).unregisterActivityLifecycleCallbacks(PresenterManager.activityLifecycleCallbacks);
}
Also used : Activity(android.app.Activity) Application(android.app.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Application (android.app.Application)125 Test (org.junit.Test)27 Context (android.content.Context)24 Activity (android.app.Activity)23 ApplicationInfo (android.content.pm.ApplicationInfo)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 File (java.io.File)10 MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)8 Before (org.junit.Before)8 ShadowApplication (org.robolectric.shadows.ShadowApplication)8 MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 PrintWriter (java.io.PrintWriter)6 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 RemoteException (android.os.RemoteException)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 AndroidRuntimeException (android.util.AndroidRuntimeException)5