Java Code Examples for android.content.Context#getApplicationContext()

The following examples show how to use android.content.Context#getApplicationContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatusBarUtil.java    From LeisureRead with Apache License 2.0 6 votes vote down vote up
/**
 * Default status dp = 24 or 25
 * mhdpi = dp * 1
 * hdpi = dp * 1.5
 * xhdpi = dp * 2
 * xxhdpi = dp * 3
 * eg : 1920x1080, xxhdpi, => status/all = 25/640(dp) = 75/1080(px)
 * <p/>
 * don't forget toolbar's dp = 48
 *
 * @return px
 */
@IntRange(from = 0, to = 75)
public static int getStatusBarOffsetPx(Context context) {

  if (isLessKitkat()) {
    return 0;
  }
  Context appContext = context.getApplicationContext();
  int result = 0;
  int resourceId =
      appContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
    result = appContext.getResources().getDimensionPixelSize(resourceId);
  }
  return result;
}
 
Example 2
Source File: Settings.java    From Abelana-Android with Apache License 2.0 6 votes vote down vote up
static void publishInstallAsync(final Context context, final String applicationId,
    final Request.Callback callback) {
    // grab the application context ahead of time, since we will return to the caller immediately.
    final Context applicationContext = context.getApplicationContext();
    Settings.getExecutor().execute(new Runnable() {
        @Override
        public void run() {
            final Response response = Settings.publishInstallAndWaitForResponse(applicationContext, applicationId, false);
            if (callback != null) {
                // invoke the callback on the main thread.
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onCompleted(response);
                    }
                });
            }
        }
    });
}
 
Example 3
Source File: LocationCollectionClient.java    From mapbox-events-android with MIT License 6 votes vote down vote up
/**
 * Install location collection client
 *
 * @param context         non-null reference to context object.
 * @param defaultInterval default session rotation interval.
 * @return instance of location collector client
 */
public static LocationCollectionClient install(@NonNull Context context, long defaultInterval) {
  Context applicationContext;
  if (context.getApplicationContext() == null) {
    // In shared processes content providers getApplicationContext() can return null.
    applicationContext = context;
  } else {
    applicationContext = context.getApplicationContext();
  }

  synchronized (lock) {
    if (locationCollectionClient == null) {
      locationCollectionClient = new LocationCollectionClient(new LocationEngineControllerImpl(applicationContext,
        LocationEngineProvider.getBestLocationEngine(applicationContext), new LocationUpdatesBroadcastReceiver()),
        new HandlerThread("LocationSettingsChangeThread"),
        new SessionIdentifier(defaultInterval),
        applicationContext.getSharedPreferences(MAPBOX_SHARED_PREFERENCES, Context.MODE_PRIVATE),
        // Provide empty token as it is not available yet
        new MapboxTelemetry(applicationContext, "",
          String.format("%s/%s", LOCATION_COLLECTOR_USER_AGENT, BuildConfig.VERSION_NAME)));
    }
  }
  return locationCollectionClient;
}
 
Example 4
Source File: BoxSession.java    From box-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Create a BoxSession using a specific box clientId, secret, and redirectUrl. This constructor is not necessary unless
 * an application uses multiple api keys.
 * Note: When setting the userId to null ui will be shown to ask which user to authenticate as if at least one user is logged in. If no
 * user has been stored will show login ui.
 *
 * @param context      current context.
 * @param clientId     the developer's client id to access the box api.
 * @param clientSecret the developer's secret used to interpret the response coming from Box.
 * @param redirectUrl  the developer's redirect url to use for authenticating via Box.
 * @param userId       user id to login as or null to login as a new user.
 */
public BoxSession(Context context, String userId, String clientId, String clientSecret, String redirectUrl) {
    mClientId = clientId;
    mClientSecret = clientSecret;
    mClientRedirectUrl = redirectUrl;
    if (getRefreshProvider() == null && (SdkUtils.isEmptyString(mClientId) || SdkUtils.isEmptyString(mClientSecret))) {
        throw new RuntimeException("Session must have a valid client id and client secret specified.");
    }
    mApplicationContext = context.getApplicationContext();
    if (!SdkUtils.isEmptyString(userId)) {
        mAuthInfo = BoxAuthentication.getInstance().getAuthInfo(userId, context);
        mUserId = userId;
    }
    if (mAuthInfo == null) {
        mUserId = userId;
        mAuthInfo = new BoxAuthentication.BoxAuthenticationInfo();
    }
    mAuthInfo.setClientId(mClientId);
    setupSession();
}
 
Example 5
Source File: AdvertisementManager.java    From HeartbeatFixerForGCM with Apache License 2.0 5 votes vote down vote up
private AdvertisementManager(@NonNull Context context) {
    mContext = context.getApplicationContext();
    mPreferences = mContext.getSharedPreferences("ad", Context.MODE_PRIVATE);

    mLocalAdVersion = mPreferences.getInt("version", 0);

    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    mGson = new Gson();
}
 
Example 6
Source File: AccountManager.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
private void resetDcContext(Context context) {
    // create an empty DcContext object - this will be set up then, starting with
    // getSelectedAccount()
    ApplicationContext appContext = (ApplicationContext)context.getApplicationContext();
    appContext.dcContext.notificationCenter.removeAllNotifiations();
    appContext.dcContext.stopIo();
    appContext.dcContext.unref();
    appContext.dcContext = new ApplicationDcContext(context);
}
 
Example 7
Source File: SpeekerToast.java    From sctalk with Apache License 2.0 5 votes vote down vote up
public static void show(Context context, CharSequence text, int duration) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    View view = inflater.inflate(R.layout.tt_speeker_layout, null);
    TextView title = (TextView) view.findViewById(R.id.top_tip);
    title.setText(text);
    Toast toast = new Toast(context.getApplicationContext());
    toast.setGravity(
            Gravity.FILL_HORIZONTAL | Gravity.TOP,
            0,
            (int) context.getResources().getDimension(
                    R.dimen.top_bar_default_height));
    toast.setDuration(duration);
    toast.setView(view);
    toast.show();
}
 
Example 8
Source File: MusicPlaybackQueueStore.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param context The {@link Context} to use
 * @return A new instance of this class.
 */
@NonNull
public static synchronized MusicPlaybackQueueStore getInstance(@NonNull final Context context) {
    if (sInstance == null) {
        sInstance = new MusicPlaybackQueueStore(context.getApplicationContext());
    }
    return sInstance;
}
 
Example 9
Source File: DatabaseFactory.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
    this.context = context.getApplicationContext();
}
 
Example 10
Source File: YubiKitManager.java    From yubikit-android with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize instance of {@link YubiKitManager}
 * @param context application context
 * @param handler on which callbacks will be invoked (default is main thread)
 */
public YubiKitManager(Context context, @Nullable Handler handler) {
    this(handler != null ? handler : new Handler(Looper.getMainLooper()),
            new UsbDeviceManager(context.getApplicationContext()),
            new NfcDeviceManager(context.getApplicationContext()));
}
 
Example 11
Source File: Preferences.java    From ClassSchedule with Apache License 2.0 4 votes vote down vote up
public static void init(Context context) {
    Preferences.context = context.getApplicationContext();
    configFileName = context.getPackageName();
}
 
Example 12
Source File: NetworkChangeNotifier.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private NetworkChangeNotifier(Context context) {
    mContext = context.getApplicationContext();
    mNativeChangeNotifiers = new ArrayList<Integer>();
    mConnectionTypeObservers = new ObserverList<ConnectionTypeObserver>();
}
 
Example 13
Source File: AssetUpdater.java    From memetastic with GNU General Public License v3.0 4 votes vote down vote up
public LoadAssetsThread(Context context) {
    _context = context.getApplicationContext();
    _appSettings = AppSettings.get();
    _tagKeys = context.getResources().getStringArray(R.array.meme_tags__keys);
}
 
Example 14
Source File: DefaultCrashProcess.java    From BaseUIFrame with MIT License 4 votes vote down vote up
public DefaultCrashProcess(Context context) {
    mContext = context.getApplicationContext();
}
 
Example 15
Source File: VideoExoPlayer.java    From TigerVideo with Apache License 2.0 4 votes vote down vote up
public VideoExoPlayer(Context context) {

        mContext = context.getApplicationContext();
        initExoPlayer();
    }
 
Example 16
Source File: SettingsSignal.java    From opentasks with Apache License 2.0 4 votes vote down vote up
public SettingsSignal(Context context)
{
    mContext = context.getApplicationContext();
}
 
Example 17
Source File: AdvertisingInfoProvider.java    From letv with Apache License 2.0 4 votes vote down vote up
public AdvertisingInfoProvider(Context context) {
    this.context = context.getApplicationContext();
    this.preferenceStore = new PreferenceStoreImpl(context, ADVERTISING_INFO_PREFERENCES);
}
 
Example 18
Source File: SmallWidgetProvider.java    From IdealMedia with Apache License 2.0 4 votes vote down vote up
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    mContext = context;
    final App app = (App)context.getApplicationContext();
    final PlayerService service = app.getService();

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.notification);

            ComponentName componentName = new ComponentName(mContext, PlayerService.class);

            for (int currentAppWidgetId : appWidgetIds) {
                Intent intentNext = new Intent(PlayerService.NEXT);
                intentNext.setComponent(componentName);
                views.setOnClickPendingIntent(R.id.action_next, PendingIntent.getService(mContext, 0, intentNext, 0));

                Intent intentPrev = new Intent(PlayerService.PREV);
                intentPrev.setComponent(componentName);
                views.setOnClickPendingIntent(R.id.action_prev, PendingIntent.getService(mContext, 0, intentPrev, 0));

                Intent intentPlay = new Intent(PlayerService.PLAY);
                intentPlay.setComponent(componentName);
                views.setOnClickPendingIntent(R.id.action_play, PendingIntent.getService(mContext, 0, intentPlay, 0));

                Intent intentOpen = new Intent(context, NavigationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intentOpen, 0);
                views.setOnClickPendingIntent(R.id.notifAlbum, pendingIntent);
                views.setOnClickPendingIntent(R.id.notifTitle, pendingIntent);
                views.setOnClickPendingIntent(R.id.notifArtist, pendingIntent);

                if (service != null) {
                    Track track = service.currentTrack;

                    views.setImageViewResource(R.id.action_play, service.isPlaying() ? R.drawable.ic_pause_normal : R.drawable.ic_play_normal);

                    if (track != null) {
                        views.setTextViewText(R.id.notifTitle, track.getTitle());
                        views.setTextViewText(R.id.notifArtist, track.getArtist());

                        Bitmap cover =  MediaUtils.getArtworkQuick(context, track, 180, 180);
                        if (cover != null)
                            views.setImageViewBitmap(R.id.notifAlbum, cover);
                        else
                            views.setImageViewResource(R.id.notifAlbum,  R.drawable.ic_launcher);
                    }
                } else {
                    views.setTextViewText(R.id.notifTitle, "-");
                    views.setTextViewText(R.id.notifArtist, "");
                    views.setImageViewResource(R.id.notifAlbum,  R.drawable.ic_launcher);
                    views.setImageViewResource(R.id.action_play, R.drawable.ic_play_normal);
                }

                try {
                    appWidgetManager.updateAppWidget(currentAppWidgetId, views);
                } catch (Exception ignored) { }
            }

            return null;
        }
    }.execute();
}
 
Example 19
Source File: PixelActivityUnion.java    From Pixel-Activity-Keep-Alive with Apache License 2.0 4 votes vote down vote up
public static PixelActivityUnion with(Context context) {
	mPixelActivityUnion.mContext = context.getApplicationContext();
	return mPixelActivityUnion;
}
 
Example 20
Source File: Paper.java    From Paper with Apache License 2.0 2 votes vote down vote up
/**
 * Lightweight method to init Paper instance. Should be executed in {@link Application#onCreate()}
 * or {@link android.app.Activity#onCreate(Bundle)}.
 * <p/>
 *
 * @param context context, used to get application context
 */
public static void init(Context context) {
    mContext = context.getApplicationContext();
}