Search in sources :

Example 1 with ContextScope

use of roboguice.inject.ContextScope in project roboguice by roboguice.

the class RoboSherlockPreferenceActivity method setPreferenceScreen.

@SuppressWarnings("deprecation")
@Override
public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
    super.setPreferenceScreen(preferenceScreen);
    final ContextScope scope = RoboGuice.getInjector(this).getInstance(ContextScope.class);
    synchronized (ContextScope.class) {
        scope.enter(this);
        try {
            preferenceListener.injectPreferenceViews();
        } finally {
            scope.exit(this);
        }
    }
}
Also used : ContextScope(roboguice.inject.ContextScope)

Example 2 with ContextScope

use of roboguice.inject.ContextScope in project roboguice by roboguice.

the class RoboPreferenceActivity method setPreferenceScreen.

@Override
@Deprecated
public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
    super.setPreferenceScreen(preferenceScreen);
    final ContextScope scope = RoboGuice.getInjector(this).getInstance(ContextScope.class);
    synchronized (ContextScope.class) {
        scope.enter(this);
        try {
            preferenceListener.injectPreferenceViews();
        } finally {
            scope.exit(this);
        }
    }
}
Also used : ContextScope(roboguice.inject.ContextScope)

Example 3 with ContextScope

use of roboguice.inject.ContextScope in project scdl by passy.

the class AdViewManagerTest method setUp.

@Before
public void setUp() {
    mActivity = Robolectric.buildActivity(Activity.class).create().get();
    final ApplicationPreferences preferences = new ApplicationPreferences() {

        @Override
        public boolean isAdFree() {
            return mAdFree;
        }
    };
    AbstractModule module = new AbstractModule() {

        @Override
        protected void configure() {
            bind(ApplicationPreferences.class).toInstance(preferences);
            bind(ActivityLayoutInflater.class).toInstance(new TestLayoutInflater(mActivity));
        }
    };
    final ContextScope contextScope = new ContextScope(Robolectric.application);
    contextScope.enter(mActivity);
    try {
        TestHelper.overridenInjector(this, module);
    } finally {
        contextScope.exit(mActivity);
    }
}
Also used : ActivityLayoutInflater(net.rdrei.android.scdl2.guice.ActivityLayoutInflater) ContextScope(roboguice.inject.ContextScope) Activity(android.app.Activity) ApplicationPreferences(net.rdrei.android.scdl2.ApplicationPreferences) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 4 with ContextScope

use of roboguice.inject.ContextScope in project roboguice by roboguice.

the class DefaultRoboModule method configure.

/**
     * Configure this module to define Android related bindings.
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void configure() {
    final Provider<Context> contextProvider = getProvider(Context.class);
    final EventListenerThreadingDecorator observerThreadingDecorator = new EventListenerThreadingDecorator();
    // Singletons
    bind(ViewListener.class).toInstance(viewListener);
    // ContextSingleton bindings
    bindScope(ContextSingleton.class, contextScope);
    //we need to super bind as we inject the scope by code only, not by annotations
    superbind(ContextScope.class).toInstance(contextScope);
    bind(AssetManager.class).toProvider(AssetManagerProvider.class);
    bind(Context.class).toProvider(NullProvider.<Context>instance()).in(ContextSingleton.class);
    bind(Activity.class).toProvider(NullProvider.<Activity>instance()).in(ContextSingleton.class);
    bind(RoboActivity.class).toProvider(NullProvider.<RoboActivity>instance()).in(ContextSingleton.class);
    bind(Service.class).toProvider(NullProvider.<Service>instance()).in(ContextSingleton.class);
    bind(RoboService.class).toProvider(NullProvider.<RoboService>instance()).in(ContextSingleton.class);
    // Sundry Android Classes
    bind(SharedPreferences.class).toProvider(SharedPreferencesProvider.class);
    bind(Resources.class).toProvider(ResourcesProvider.class);
    bind(ContentResolver.class).toProvider(ContentResolverProvider.class);
    bind(Application.class).toInstance(application);
    bind(EventListenerThreadingDecorator.class).toInstance(observerThreadingDecorator);
    bind(EventManager.class).annotatedWith(Names.named(GLOBAL_EVENT_MANAGER_NAME)).to(EventManager.class).asEagerSingleton();
    bind(Handler.class).toProvider(HandlerProvider.class);
    // System Services
    for (Entry<Class, String> entry : mapSystemSericeClassToName.entrySet()) {
        bindSystemService(entry.getKey(), entry.getValue());
    }
    // System Services that must be scoped to current context
    bind(LayoutInflater.class).toProvider(new ContextScopedSystemServiceProvider<LayoutInflater>(contextProvider, Context.LAYOUT_INFLATER_SERVICE));
    bind(SearchManager.class).toProvider(new ContextScopedSystemServiceProvider<SearchManager>(contextProvider, Context.SEARCH_SERVICE));
    // Android Resources, Views and extras require special handling
    if (hasInjectionPointsForAnnotation(InjectResource.class)) {
        bindListener(Matchers.any(), resourceListener);
    }
    if (hasInjectionPointsForAnnotation(InjectExtra.class)) {
        final ExtrasListener extrasListener = new ExtrasListener(contextProvider);
        bindListener(Matchers.any(), extrasListener);
    }
    //should be bound only if we use InjectView or InjectFragment
    bindListener(Matchers.any(), viewListener);
    final PreferenceListener preferenceListener = new PreferenceListener(contextProvider, application);
    superbind(PreferenceListener.class).toInstance(preferenceListener);
    if (hasInjectionPointsForAnnotation(InjectPreference.class)) {
        bindListener(Matchers.any(), preferenceListener);
    }
    //should always be bound as ContentViewListener relies on event system
    bindListener(Matchers.any(), new ObservesTypeListener(getProvider(EventManager.class), observerThreadingDecorator));
    requestInjection(observerThreadingDecorator);
    if (isInjectable(Ln.class)) {
        bind(LnInterface.class).to(LnImpl.class);
        //should this be placed in if statement ?
        requestStaticInjection(Ln.class);
    }
    bindDynamicBindings();
}
Also used : RoboService(roboguice.service.RoboService) ViewListener(roboguice.inject.ViewListener) RoboActivity(roboguice.activity.RoboActivity) Activity(android.app.Activity) ContentResolver(android.content.ContentResolver) RoboActivity(roboguice.activity.RoboActivity) Context(android.content.Context) ObservesTypeListener(roboguice.event.ObservesTypeListener) AssetManager(android.content.res.AssetManager) EventManager(roboguice.event.EventManager) EventListenerThreadingDecorator(roboguice.event.eventListener.factory.EventListenerThreadingDecorator) SharedPreferences(android.content.SharedPreferences) SearchManager(android.app.SearchManager) ExtrasListener(roboguice.inject.ExtrasListener) ContextScope(roboguice.inject.ContextScope) Service(android.app.Service) RoboService(roboguice.service.RoboService) Handler(android.os.Handler) PreferenceListener(roboguice.inject.PreferenceListener) LnInterface(roboguice.util.LnInterface) LayoutInflater(android.view.LayoutInflater) Resources(android.content.res.Resources) Application(android.app.Application)

Aggregations

ContextScope (roboguice.inject.ContextScope)4 Activity (android.app.Activity)2 Application (android.app.Application)1 SearchManager (android.app.SearchManager)1 Service (android.app.Service)1 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 AssetManager (android.content.res.AssetManager)1 Resources (android.content.res.Resources)1 Handler (android.os.Handler)1 LayoutInflater (android.view.LayoutInflater)1 AbstractModule (com.google.inject.AbstractModule)1 ApplicationPreferences (net.rdrei.android.scdl2.ApplicationPreferences)1 ActivityLayoutInflater (net.rdrei.android.scdl2.guice.ActivityLayoutInflater)1 Before (org.junit.Before)1 RoboActivity (roboguice.activity.RoboActivity)1 EventManager (roboguice.event.EventManager)1 ObservesTypeListener (roboguice.event.ObservesTypeListener)1 EventListenerThreadingDecorator (roboguice.event.eventListener.factory.EventListenerThreadingDecorator)1