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

The following examples show how to use android.content.Context#getPackageName() . 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: Volley.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 */
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        stack = new HurlStack();
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}
 
Example 2
Source File: PlacesWidgetProvider.java    From protrip with MIT License 6 votes vote down vote up
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int i = 0; i < appWidgetIds.length; i++) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_collection);

        Intent intent = new Intent(context, PlacesWidgetRemoteViewsService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.widget_list, intent);

        Intent toastIntent = new Intent(context, PlacesWidgetRemoteViewsService.class);
        toastIntent.setAction(PlacesWidgetProvider.TOAST_ACTION);
        toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.widget_list, toastPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}
 
Example 3
Source File: TableWidgetProvider.java    From android with Apache License 2.0 6 votes vote down vote up
@Override
protected RemoteViews buildErrorLayout(AppWidgetManager awm, Context context, int appWidgetId, String error) {
    Intent intent = new Intent(Extension.ACTION_MAIN_SCREEN);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Intent retryIntent = new Intent(context, getWidgetClass());
    PendingIntent retryPendingIntent = PendingIntent.getBroadcast(context, 0, retryIntent, 0);

    RemoteViews rv = new RemoteViews(context.getPackageName(), getWidgetLayout());
    rv.setOnClickPendingIntent(R.id.widget_header, pendingIntent);
    rv.setOnClickPendingIntent(R.id.btn_retry, retryPendingIntent);
    rv.setViewVisibility(R.id.progress_bar, View.GONE);
    rv.setViewVisibility(R.id.btn_retry, View.VISIBLE);
    rv.setViewVisibility(R.id.prayerlist, View.GONE);

    return rv;
}
 
Example 4
Source File: GetAwayNotificationListenerService.java    From timecat with Apache License 2.0 6 votes vote down vote up
public static boolean enableNotificationListenerComponent(Context paramContext, boolean paramBoolean) {
    try {
        if (Build.VERSION.SDK_INT < 18) {
            return false;
        }
        if (paramContext != null) {
            PackageManager localPackageManager = paramContext.getPackageManager();
            ComponentName componentName = new ComponentName(paramContext.getPackageName(), GetAwayNotificationListenerService.class.getName());
            if (paramBoolean) {
                localPackageManager.setComponentEnabledSetting(componentName, 1, 1);
            } else {
                localPackageManager.setComponentEnabledSetting(componentName, 2, 1);
            }
        }
        return true;
    } catch (Throwable e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 5
Source File: UserAgent.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
private static String getVersion(final Context context) {
    final String packageName = context.getPackageName();
    try {
        return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
    } catch (PackageManager.NameNotFoundException | RuntimeException e) {
        return UNKNOWN;
    }
}
 
Example 6
Source File: ClementineWidgetProvider.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];
        appWidgetManager.getAppWidgetInfo(appWidgetId);

        // Get the layout for the App Widget and update fields
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_clementine);

        switch (mCurrentClementineAction) {
            case DEFAULT:
            case CONNECTION_STATUS:
                updateViewsOnConnectionStatusChange(context, views);
                break;
            case STATE_CHANGE:
                updateViewsOnStateChange(context, views);
                break;
        }

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
 
Example 7
Source File: NotificationListenerService.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isListeningAuthorized(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
    String packageName = context.getPackageName();

    return !(enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName));
}
 
Example 8
Source File: TelemetryUtils.java    From mapbox-events-android with MIT License 5 votes vote down vote up
private static String obtainApplicationIdentifier(Context context) {
  try {
    String packageName = context.getPackageName();
    PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
    String appIdentifier = String.format(DEFAULT_LOCALE, THREE_STRING_FORMAT, packageName,
      packageInfo.versionName, packageInfo.versionCode);

    return appIdentifier;
  } catch (Exception exception) {
    return EMPTY_STRING;
  }
}
 
Example 9
Source File: GCMBroadcastReceiver.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            com.google.cloud.backend.GCMIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
 
Example 10
Source File: LeftRightWidget.java    From homescreenarcade with GNU General Public License v3.0 5 votes vote down vote up
protected void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.left_right_widget);
    views.setOnClickPendingIntent(R.id.left_btn,
            PendingIntent.getBroadcast(context, 0, new Intent(ArcadeCommon.ACTION_LEFT), 0));
    views.setOnClickPendingIntent(R.id.right_btn,
            PendingIntent.getBroadcast(context, 0, new Intent(ArcadeCommon.ACTION_RIGHT), 0));
            
    appWidgetManager.updateAppWidget(appWidgetId, views);
}
 
Example 11
Source File: AppEventsLogger.java    From Klyph with MIT License 5 votes vote down vote up
private static SessionEventsState getSessionEventsState(Context context, AccessTokenAppIdPair accessTokenAppId) {
    synchronized (staticLock) {
        SessionEventsState state = stateMap.get(accessTokenAppId);
        if (state == null) {
            // Retrieve attributionId, but we will only send it if attribution is supported for the app.
            String attributionId = Settings.getAttributionId(context.getContentResolver());

            state = new SessionEventsState(attributionId, context.getPackageName(), hashedDeviceAndAppId);
            stateMap.put(accessTokenAppId, state);
        }
        return state;
    }
}
 
Example 12
Source File: AppStarter.java    From cordova-plugin-autostart with Apache License 2.0 5 votes vote down vote up
public void run(Context context, Intent intent, int componentState, boolean onAutostart) {
    // Enable or Disable UserPresentReceiver (or bypass the modification)
    //Log.d("Cordova AppStarter", "UserPresentReceiver component, new state:" + String.valueOf(componentState));
    if( componentState != BYPASS_USERPRESENT_MODIFICATION ) {
        ComponentName receiver = new ComponentName(context, UserPresentReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver, componentState, PackageManager.DONT_KILL_APP);
    }

    // Starting your app...
    //Log.d("Cordova AppStarter", "STARTING APP...");
    SharedPreferences sp = context.getSharedPreferences(AutoStart.PREFS, Context.MODE_PRIVATE);
    String packageName = context.getPackageName();
    String activityClassName = sp.getString(AutoStart.ACTIVITY_CLASS_NAME, "");
    if( !activityClassName.equals("") ){
        //Log.d("Cordova AppStarter", className);
        Intent activityIntent = new Intent();
        activityIntent.setClassName(
            context, String.format("%s.%s", packageName, activityClassName));
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (onAutostart) {
          activityIntent.putExtra(CORDOVA_AUTOSTART, true);
        }
        context.startActivity(activityIntent);
    }
    // Start a service in the background.
    String serviceClassName = sp.getString(AutoStart.SERVICE_CLASS_NAME, "");
    String servicePackageName = serviceClassName.substring(0, serviceClassName.lastIndexOf("."));
    if ( !serviceClassName.equals("") ) {
        Intent serviceIntent = new Intent();
        serviceIntent.setClassName(servicePackageName, serviceClassName);
        if ( onAutostart ) {
            serviceIntent.putExtra(CORDOVA_AUTOSTART, true);
        }
        context.startService(serviceIntent);
    }

}
 
Example 13
Source File: AppUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 得到软件显示版本信息
 * @param context 上下文
 * @return 当前版本信息
 */
public static String getVerName(Context context) {
    if (context == null) return "";
    String verName = "";
    try {
        String packageName = context.getPackageName();
        verName = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        Logger.e(e);
    }
    return verName;
}
 
Example 14
Source File: ClockWidget.java    From Travel-Mate with MIT License 5 votes vote down vote up
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    String mTimezoneCode = ClockWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
    TimeZone targetTimezone = TimeZone.getTimeZone(mTimezoneCode);
    views.setTextViewText(R.id.tView_timezone, targetTimezone.getID());
    views.setString(R.id.txtClockDigitalClock, "setTimeZone",  mTimezoneCode);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}
 
Example 15
Source File: UncachedInputMethodManagerUtils.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the IME specified by the context is enabled.
 * CAVEAT: This may cause a round trip IPC.
 *
 * @param context package context of the IME to be checked.
 * @param imm the {@link InputMethodManager}.
 * @return true if this IME is enabled.
 */
public static boolean isThisImeEnabled(final Context context,
        final InputMethodManager imm) {
    final String packageName = context.getPackageName();
    for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
        if (packageName.equals(imi.getPackageName())) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: VenvyFileUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
private static File getExternalCacheDir(Context context) {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
    File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            return null;
        }
        try {
            new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {
        }
    }
    return appCacheDir;
}
 
Example 17
Source File: ListWidgetProvider.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
private RemoteViews configSmall(Context context, SparseArray<PendingIntent> pendingIntentsMap) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout_small);
    views.setOnClickPendingIntent(R.id.iv_launch_app, pendingIntentsMap.get(R.id.iv_launch_app));
    return views;
}
 
Example 18
Source File: FakeR.java    From reacteu-app with MIT License 4 votes vote down vote up
public FakeR(Context context) {
	this.context = context;
	packageName = context.getPackageName();
}
 
Example 19
Source File: BUtility.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 获取引擎加密H5文件的读取协议头,与ACEContentProvider对应
 * 形如:content://com.appcan.packagename.sp/android_asset/
 */
public static String getACEContentProviderPrefix(Context context){
    String packageName = context.getPackageName();
    String contentPrefix = "content://" + packageName + ".sp/android_asset/";
    return contentPrefix;
}
 
Example 20
Source File: HostUtil.java    From GPT with Apache License 2.0 3 votes vote down vote up
/**
 * 获取插件宿主的包名
 *
 * @param context 插件context
 * @return 宿主包名,如果插件是独立安装运行,则返回插件自身包名
 */
public static String getHostPackageName(Context context) {

    String pkgName = null;

    if (context == null) {
        return pkgName;
    }

    Context appContext = getHostApplicationContextPrivate(context);

    pkgName = appContext.getPackageName();

    return pkgName;
}