Java Code Examples for com.google.inject.Provider

Following code examples demonstrate how to use com.google.inject.Providerfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use com.google.inject.Providerand various code implementation of this class.

    protected void configure() {
        final com.google.inject.Provider<Context> provider = this.getProvider(Context.class);
        final ExtrasListener extrasListener = new ExtrasListener(provider);
        final PreferenceListener preferenceListener = new PreferenceListener(provider, this.application, this.contextScope);
        final EventListenerThreadingDecorator eventListenerThreadingDecorator = new EventListenerThreadingDecorator();
        final String string = Settings$Secure.getString(this.application.getContentResolver(), "android_id");
        try {
            this.bind(PackageInfo.class).toInstance(this.application.getPackageManager().getPackageInfo(this.application.getPackageName(), 0));
            if (Strings.notEmpty(string)) {
                this.bindConstant().annotatedWith(Names.named("android_id")).to(string);
            }
            this.bind(ViewListener.class).toInstance(this.viewListener);
            this.bind(PreferenceListener.class).toInstance(preferenceListener);
            this.bindScope(ContextSingleton.class, this.contextScope);
            this.bind(ContextScope.class).toInstance(this.contextScope);
            this.bind(AssetManager.class).toProvider((Class<? extends Provider<?>>)AssetManagerProvider.class);
            this.bind(Context.class).toProvider((Key<? extends Provider<?>>)Key.get((TypeLiteral<? extends Provider<? extends T>>)new TypeLiteral<NullProvider<Context>>() {})).in(ContextSingleton.class);
            this.bind(Activity.class).toProvider((Key<? extends Provider<?>>)Key.get((TypeLiteral<? extends Provider<? extends T>>)new TypeLiteral<NullProvider<Activity>>() {})).in(ContextSingleton.class);
            this.bind(RoboActivity.class).toProvider((Key<? extends Provider<?>>)Key.get((TypeLiteral<? extends Provider<? extends T>>)new TypeLiteral<NullProvider<RoboActivity>>() {})).in(ContextSingleton.class);
            this.bind(Service.class).toProvider((Key<? extends Provider<?>>)Key.get((TypeLiteral<? extends Provider<? extends T>>)new TypeLiteral<NullProvider<Service>>() {})).in(ContextSingleton.class);
            this.bind(RoboService.class).toProvider((Key<? extends Provider<?>>)Key.get((TypeLiteral<? extends Provider<? extends T>>)new TypeLiteral<NullProvider<RoboService>>() {})).in(ContextSingleton.class);
            this.bind(SharedPreferences.class).toProvider((Class<? extends Provider<?>>)SharedPreferencesProvider.class);
            this.bind(Resources.class).toProvider((Class<? extends Provider<?>>)ResourcesProvider.class);
            this.bind(ContentResolver.class).toProvider((Class<? extends Provider<?>>)ContentResolverProvider.class);
            this.bind(Application.class).toInstance(this.application);
            this.bind(EventListenerThreadingDecorator.class).toInstance(eventListenerThreadingDecorator);
            this.bind(Handler.class).toProvider((Class<? extends Provider<?>>)HandlerProvider.class);
            this.bind(LocationManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("location"));
            this.bind(WindowManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("window"));
            this.bind(ActivityManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("activity"));
            this.bind(PowerManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("power"));
            this.bind(AlarmManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("alarm"));
            this.bind(NotificationManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("notification"));
            this.bind(KeyguardManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("keyguard"));
            this.bind(Vibrator.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("vibrator"));
            this.bind(ConnectivityManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("connectivity"));
            this.bind(WifiManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("wifi"));
            this.bind(InputMethodManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("input_method"));
            this.bind(SensorManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("sensor"));
            this.bind(TelephonyManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("phone"));
            this.bind(AudioManager.class).toProvider((com.google.inject.Provider<?>)new SystemServiceProvider<Object>("audio"));
            this.bind(LayoutInflater.class).toProvider((com.google.inject.Provider<?>)new ContextScopedSystemServiceProvider<Object>(provider, "layout_inflater"));
            this.bind(SearchManager.class).toProvider((com.google.inject.Provider<?>)new ContextScopedSystemServiceProvider<Object>(provider, "search"));
            this.bindListener(Matchers.any(), this.resourceListener);
            this.bindListener(Matchers.any(), extrasListener);
            this.bindListener(Matchers.any(), this.viewListener);
            this.bindListener(Matchers.any(), preferenceListener);
            this.bindListener(Matchers.any(), new ObservesTypeListener(this.getProvider(EventManager.class), eventListenerThreadingDecorator));
            this.requestInjection(eventListenerThreadingDecorator);
            this.requestStaticInjection(Ln.class);
            if (DefaultRoboModule.fragmentManagerClass != null) {
                this.bind((Class<Object>)DefaultRoboModule.fragmentManagerClass).toProvider(FragmentManagerProvider.class);
            }
            if (Build$VERSION.SDK_INT >= 5) {
                this.bind((Class<Object>)DefaultRoboModule.accountManagerClass).toProvider(AccountManagerProvider.class);
            }
        }
        catch (PackageManager$NameNotFoundException ex) {
            throw new RuntimeException((Throwable)ex);
        }
    } 


    static <K> List<Alias> getMapBinderAliases(Class<K> keyType, Type aClass, Iterable<Binding<?>> bindings) {
		List<Alias> aliases = Lists.newArrayList();
		NodeId toId = NodeId.newTypeId(Key.get(Types.mapOf(keyType, aClass)));
		ParameterizedType comGoogleInjectProvider = Types.newParameterizedType(Provider.class, aClass);
		ParameterizedType javaxInjectProvider = Types.newParameterizedType(javax.inject.Provider.class, aClass);
		ParameterizedType stringToComGoogleInjectProviderMapEntry = Types.newParameterizedTypeWithOwner(Map.class, Map.Entry.class, keyType, comGoogleInjectProvider);
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.setOf(stringToComGoogleInjectProviderMapEntry))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.mapOf(keyType, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.mapOf(keyType, javaxInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.mapOf(keyType, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.mapOf(keyType, aClass))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.setOf(stringToComGoogleInjectProviderMapEntry))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(javax.inject.Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		for (Binding<?> binding : bindings) {
			if (binding.getKey().getTypeLiteral().getType().equals(aClass) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
				aliases.add(new Alias(NodeId.newInstanceId(binding.getKey()), toId));
			}
			if (binding.getKey().getTypeLiteral().getType().equals(stringToComGoogleInjectProviderMapEntry) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
				aliases.add(new Alias(NodeId.newInstanceId(binding.getKey()), toId));
			}
		}
		return aliases;
	} 

    static List<Alias> getMultibinderAliases(Type aClass, Iterable<Binding<?>> bindings) {
		List<Alias> aliases = Lists.newArrayList();
		NodeId toId = NodeId.newTypeId(Key.get(Types.setOf(aClass)));
		ParameterizedType comGoogleInjectProvider = Types.newParameterizedType(Provider.class, aClass);
		ParameterizedType javaxInjectProvider = Types.newParameterizedType(javax.inject.Provider.class, aClass);
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.setOf(aClass))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.newParameterizedType(Collection.class, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, javaxInjectProvider))), toId));
		for (Binding<?> binding : bindings) {
			if (binding.getKey().getTypeLiteral().getType().equals(aClass) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
			}
		}
		return aliases;
	} 

  public static <T> com.google.inject.Provider<T> guicify(Provider<T> provider) {
    if (provider instanceof com.google.inject.Provider) {
      return (com.google.inject.Provider<T>) provider;
    }

    final Provider<T> delegate = checkNotNull(provider, "provider");
    return new com.google.inject.Provider<T>() {
      public T get() {
        return delegate.get();
      }

      @Override public String toString() {
        return "guicified(" + delegate + ")";
      }
    };
  } 

            if (type instanceof Class) {
                Class<?> beanClass = (Class) type;
                if (GetRestful.isRootResource(beanClass)) {
                    delayedBinds.put(binding.getProvider(), beanClass);
                }

                if (beanClass.isAnnotationPresent(Provider.class)) {
                    providerFactory.registerProviderInstance(binding.getProvider().get());
                }
            } 

    @Override
    public void configure(Binder binder)
    {
        binder.bind(Application.class).toProvider((com.google.inject.Provider<Application>)() -> application);
        binder.bind(UriInfo.class).toProvider((com.google.inject.Provider<UriInfo>)() -> uriInfo);
        binder.bind(HttpServletRequest.class).toProvider((com.google.inject.Provider<HttpServletRequest>)()->servletRequest);
        binder.bind(RequestUserRefProvider.class).to(RequestUserRefProviderImpl.class).asEagerSingleton();
    } 

            if (providerOrInstance instanceof Provider) {
                final Provider<?> provider = (Provider<?>) providerOrInstance;
                final com.google.inject.Provider<?> guiceProvider = Providers.guicify(provider);
                binder.bind(bindingLiteral).toProvider(guiceProvider);
            } else if (providerOrInstance instanceof com.google.inject.Provider) {
                final com.google.inject.Provider<?> guiceProvider = (com.google.inject.Provider<?>) providerOrInstance;
                binder.bind(bindingLiteral).toProvider(guiceProvider);
            } else {
                binder.bind(bindingLiteral).toInstance(providerOrInstance);
            } 

    
    protected Long getUserId()
    {
        return contextProvider.get().getUserAccount().getId();
    } 

	private User determineUser() {
		try {
			return currentUserProvider.get();
		}
		catch (Throwable e) {
			return null;
		}
	} 

  
  public static com.google.inject.Provider guicify(javax.inject.Provider paramProvider)
  {
    if ((paramProvider instanceof com.google.inject.Provider)) {
      return (com.google.inject.Provider)paramProvider;
    }
    javax.inject.Provider localProvider = (javax.inject.Provider)Preconditions.checkNotNull(paramProvider, "provider");
    Set localSet = InjectionPoint.forInstanceMethodsAndFields(paramProvider.getClass());
    if (localSet.isEmpty()) {
      return new GuicifiedProvider(localProvider, null);
    }
    HashSet localHashSet = Sets.newHashSet();
    Object localObject = localSet.iterator();
    while (((Iterator)localObject).hasNext())
    {
      InjectionPoint localInjectionPoint = (InjectionPoint)((Iterator)localObject).next();
      localHashSet.addAll(localInjectionPoint.getDependencies());
    }
    localObject = ImmutableSet.copyOf(localHashSet);
    return new GuicifiedProviderWithDependencies((Set)localObject, localProvider, null);
  } 

	public Type getKeyType() {
		Type keyType = super.getKeyType();
		if (!isInstanceOf(keyType, com.google.inject.Provider.class)) {
			if (isInstanceOf(keyType, javax.inject.Provider.class)) {
				if (!isClassBinding())
					throw new IllegalStateException("The method "+getMethod().getName()+" returns javax.inject.Provider, but this kind of binding is allowed only for com.google.inject.Provider.");
			} else {
				throw new IllegalStateException("The method "+getMethod().getName()+" is expected to return a Class<? extends Provider<Something>> or directly Provider<Something>.");
			}
		}
		return getFirstTypeParameter((ParameterizedType) keyType);
	} 

	static List<Alias> getMultibinderAliases(Type aClass, Iterable<Binding<?>> bindings) {
		List<Alias> aliases = Lists.newArrayList();
		NodeId toId = NodeId.newTypeId(Key.get(Types.setOf(aClass)));
		ParameterizedType comGoogleInjectProvider = Types.newParameterizedType(com.google.inject.Provider.class, aClass);
		ParameterizedType javaxInjectProvider = Types.newParameterizedType(Provider.class, aClass);
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.setOf(aClass))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.newParameterizedType(Collection.class, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, javaxInjectProvider))), toId));
		for (Binding<?> binding : bindings) {
			if (binding.getKey().getTypeLiteral().getType().equals(aClass) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
			}
		}
		return aliases;
	} 

	static <K> List<Alias> getMapBinderAliases(Class<K> keyType, Type aClass, Iterable<Binding<?>> bindings) {
		List<Alias> aliases = Lists.newArrayList();
		NodeId toId = NodeId.newTypeId(Key.get(Types.mapOf(keyType, aClass)));
		ParameterizedType comGoogleInjectProvider = Types.newParameterizedType(com.google.inject.Provider.class, aClass);
		ParameterizedType javaxInjectProvider = Types.newParameterizedType(Provider.class, aClass);
		ParameterizedType stringToComGoogleInjectProviderMapEntry = Types.newParameterizedTypeWithOwner(Map.class, Map.Entry.class, keyType, comGoogleInjectProvider);
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.setOf(stringToComGoogleInjectProviderMapEntry))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.mapOf(keyType, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.mapOf(keyType, javaxInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.mapOf(keyType, comGoogleInjectProvider))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.mapOf(keyType, aClass))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.setOf(stringToComGoogleInjectProviderMapEntry))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		aliases.add(new Alias(NodeId.newTypeId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(com.google.inject.Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		aliases.add(new Alias(NodeId.newInstanceId(Key.get(Types.newParameterizedType(Collection.class, Types.newParameterizedType(com.google.inject.Provider.class, stringToComGoogleInjectProviderMapEntry)))), toId));
		for (Binding<?> binding : bindings) {
			if (binding.getKey().getTypeLiteral().getType().equals(aClass) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
				aliases.add(new Alias(NodeId.newInstanceId(binding.getKey()), toId));
			}
			if (binding.getKey().getTypeLiteral().getType().equals(stringToComGoogleInjectProviderMapEntry) && binding.getKey().getAnnotationType() != null && binding.getKey().getAnnotationType().getName().equals("com.google.inject.multibindings.Element")) {
				aliases.add(new Alias(NodeId.newTypeId(binding.getKey()), toId));
				aliases.add(new Alias(NodeId.newInstanceId(binding.getKey()), toId));
			}
		}
		return aliases;
	}