Example usage for android.content Context setTheme

List of usage examples for android.content Context setTheme

Introduction

In this page you can find the example usage for android.content Context setTheme.

Prototype

public abstract void setTheme(@StyleRes int resid);

Source Link

Document

Set the base theme for this context.

Usage

From source file:com.android.settings.PreviewPagerAdapter.java

public PreviewPagerAdapter(Context context, boolean isLayoutRtl, int[] previewSampleResIds,
        Configuration[] configurations) {
    mIsLayoutRtl = isLayoutRtl;//from  w w w .jav a2 s.  c o m
    mPreviewFrames = new FrameLayout[previewSampleResIds.length];
    mViewStubInflated = new boolean[previewSampleResIds.length][configurations.length];

    for (int i = 0; i < previewSampleResIds.length; ++i) {
        int p = mIsLayoutRtl ? previewSampleResIds.length - 1 - i : i;
        mPreviewFrames[p] = new FrameLayout(context);
        mPreviewFrames[p].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

        for (int j = 0; j < configurations.length; ++j) {
            // Create a new configuration for the specified value. It won't
            // have any theme set, so manually apply the current theme.
            final Context configContext = context.createConfigurationContext(configurations[j]);
            configContext.setTheme(context.getThemeResId());

            final LayoutInflater configInflater = LayoutInflater.from(configContext);
            final ViewStub sampleViewStub = new ViewStub(configContext);
            sampleViewStub.setLayoutResource(previewSampleResIds[i]);
            final int fi = i, fj = j;
            sampleViewStub.setOnInflateListener(new OnInflateListener() {
                @Override
                public void onInflate(ViewStub stub, View inflated) {
                    inflated.setVisibility(stub.getVisibility());
                    mViewStubInflated[fi][fj] = true;
                }
            });

            mPreviewFrames[p].addView(sampleViewStub);
        }
    }
}

From source file:com.zhengde163.netguard.Receiver.java

public static void notifyNewApplication(int uid, Context context) {
    if (uid < 0)
        return;//w  w  w .jav  a  2s  .  c  o  m

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    try {
        // Get application info
        String name = TextUtils.join(", ", Util.getApplicationNames(uid, context));

        // Get application info
        PackageManager pm = context.getPackageManager();
        String[] packages = pm.getPackagesForUid(uid);
        if (packages == null || packages.length < 1)
            throw new PackageManager.NameNotFoundException(Integer.toString(uid));
        boolean internet = Util.hasInternet(uid, context);

        // Build notification
        Intent main = new Intent(context, ActivityMain.class);
        main.putExtra(ActivityMain.EXTRA_REFRESH, true);
        main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
        PendingIntent pi = PendingIntent.getActivity(context, uid, main, PendingIntent.FLAG_UPDATE_CURRENT);

        //            Util.setTheme(context);
        context.setTheme(R.style.AppThemeBlue);
        TypedValue tv = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_security_white_24dp)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(context.getString(R.string.msg_installed, name)).setContentIntent(pi)
                .setColor(tv.data).setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
        }

        // Get defaults
        SharedPreferences prefs_wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
        SharedPreferences prefs_other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
        boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true));
        boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true));

        // Build Wi-Fi action
        Intent riWifi = new Intent(context, ServiceSinkhole.class);
        riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi");
        riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi);

        PendingIntent piWifi = PendingIntent.getService(context, uid, riWifi,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
                context.getString(wifi ? R.string.title_allow : R.string.title_block), piWifi);

        // Build mobile action
        Intent riOther = new Intent(context, ServiceSinkhole.class);
        riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other");
        riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other);
        PendingIntent piOther = PendingIntent.getService(context, uid + 10000, riOther,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(other ? R.drawable.other_on : R.drawable.other_off,
                context.getString(other ? R.string.title_allow : R.string.title_block), piOther);

        // Show notification
        if (internet)
            NotificationManagerCompat.from(context).notify(uid, builder.build());
        else {
            NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
            expanded.bigText(context.getString(R.string.msg_installed, name));
            expanded.setSummaryText(context.getString(R.string.title_internet));
            NotificationManagerCompat.from(context).notify(uid, expanded.build());
        }

    } catch (PackageManager.NameNotFoundException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}

From source file:github.daneren2005.dsub.util.Util.java

public static void applyTheme(Context context, String theme) {
    if ("dark".equals(theme)) {
        context.setTheme(R.style.Theme_DSub_Dark);
    } else if ("black".equals(theme)) {
        context.setTheme(R.style.Theme_DSub_Black);
    } else if ("holo".equals(theme)) {
        context.setTheme(R.style.Theme_DSub_Holo);
    } else {/*from w ww.j a v a 2s.  co m*/
        context.setTheme(R.style.Theme_DSub_Light);
    }

    SharedPreferences prefs = Util.getPreferences(context);
    if (prefs.getBoolean(Constants.PREFERENCES_KEY_OVERRIDE_SYSTEM_LANGUAGE, false)) {
        Configuration config = new Configuration();
        config.locale = Locale.ENGLISH;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }
}

From source file:io.jawg.osmcontributor.OsmTemplateApplication.java

public void init(Context context) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    String fontSize = sharedPref.getString("font_size", "normal");
    switch (fontSize) {
    case "small":
        context.setTheme(R.style.FontSmall);
        break;//from w w w.j  a va2 s . c om
    case "normal":
        context.setTheme(R.style.FontMedium);
        break;
    case "large":
        context.setTheme(R.style.FontLarge);
        break;
    case "xlarge":
        context.setTheme(R.style.FontXLarge);
        break;
    }
}

From source file:com.geecko.QuickLyric.broadcastReceiver.MusicBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    /** Google Play Music
     //bool streaming            long position
     //long albumId               String album
     //bool currentSongLoaded      String track
     //long ListPosition         long ListSize
     //long id                  bool playing
     //long duration            int previewPlayType
     //bool supportsRating         int domain
     //bool albumArtFromService      String artist
     //int rating               bool local
     //bool preparing            bool inErrorState
     *///from ww w .  j  av a  2s . c o  m

    Bundle extras = intent.getExtras();
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean lengthFilter = sharedPref.getBoolean("pref_filter_20min", true);

    if (extras != null)
        try {
            extras.getInt("state");
        } catch (BadParcelableException e) {
            return;
        }

    if (extras == null || extras.getInt("state") > 1 //Tracks longer than 20min are presumably not songs
            || (lengthFilter && (extras.get("duration") instanceof Long && extras.getLong("duration") > 1200000)
                    || (extras.get("duration") instanceof Double && extras.getDouble("duration") > 1200000)
                    || (extras.get("duration") instanceof Integer && extras.getInt("duration") > 1200))
            || (lengthFilter && (extras.get("secs") instanceof Long && extras.getLong("secs") > 1200000)
                    || (extras.get("secs") instanceof Double && extras.getDouble("secs") > 1200000)
                    || (extras.get("secs") instanceof Integer && extras.getInt("secs") > 1200))
            || (extras.containsKey("com.maxmpz.audioplayer.source") && Build.VERSION.SDK_INT >= 19))
        return;

    String artist = extras.getString("artist");
    String track = extras.getString("track");
    long position = extras.containsKey("position") && extras.get("position") instanceof Long
            ? extras.getLong("position")
            : -1;
    if (extras.get("position") instanceof Double)
        position = Double.valueOf(extras.getDouble("position")).longValue();
    boolean isPlaying = extras.getBoolean(extras.containsKey("playstate") ? "playstate" : "playing", true);

    if (intent.getAction().equals("com.amazon.mp3.metachanged")) {
        artist = extras.getString("com.amazon.mp3.artist");
        track = extras.getString("com.amazon.mp3.track");
    } else if (intent.getAction().equals("com.spotify.music.metadatachanged"))
        isPlaying = spotifyPlaying;
    else if (intent.getAction().equals("com.spotify.music.playbackstatechanged"))
        spotifyPlaying = isPlaying;

    if ((artist == null || "".equals(artist)) //Could be problematic
            || (track == null || "".equals(track) || track.startsWith("DTNS"))) // Ignore one of my favorite podcasts
        return;

    SharedPreferences current = context.getSharedPreferences("current_music", Context.MODE_PRIVATE);
    String currentArtist = current.getString("artist", "");
    String currentTrack = current.getString("track", "");

    SharedPreferences.Editor editor = current.edit();
    editor.putString("artist", artist);
    editor.putString("track", track);
    if (!(artist.equals(currentArtist) && track.equals(currentTrack) && position == -1))
        editor.putLong("position", position);
    editor.putBoolean("playing", isPlaying);
    if (isPlaying) {
        long currentTime = System.currentTimeMillis();
        editor.putLong("startTime", currentTime);
    }
    editor.apply();

    autoUpdate = autoUpdate || sharedPref.getBoolean("pref_auto_refresh", false);
    int notificationPref = Integer.valueOf(sharedPref.getString("pref_notifications", "0"));

    if (autoUpdate && App.isActivityVisible()) {
        Intent internalIntent = new Intent("Broadcast");
        internalIntent.putExtra("artist", artist).putExtra("track", track);
        LyricsViewFragment.sendIntent(context, internalIntent);
        forceAutoUpdate(false);
    }

    boolean inDatabase = DatabaseHelper.getInstance(context)
            .presenceCheck(new String[] { artist, track, artist, track });

    if (notificationPref != 0 && isPlaying && (inDatabase || OnlineAccessVerifier.check(context))) {
        Intent activityIntent = new Intent("com.geecko.QuickLyric.getLyrics").putExtra("TAGS",
                new String[] { artist, track });
        Intent wearableIntent = new Intent("com.geecko.QuickLyric.SEND_TO_WEARABLE").putExtra("artist", artist)
                .putExtra("track", track);
        PendingIntent openAppPending = PendingIntent.getActivity(context, 0, activityIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent wearablePending = PendingIntent.getBroadcast(context, 8, wearableIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Action wearableAction = new NotificationCompat.Action.Builder(R.drawable.ic_watch,
                context.getString(R.string.wearable_prompt), wearablePending).build();

        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context);
        NotificationCompat.Builder wearableNotifBuilder = new NotificationCompat.Builder(context);

        int[] themes = new int[] { R.style.Theme_QuickLyric, R.style.Theme_QuickLyric_Red,
                R.style.Theme_QuickLyric_Purple, R.style.Theme_QuickLyric_Indigo,
                R.style.Theme_QuickLyric_Green, R.style.Theme_QuickLyric_Lime, R.style.Theme_QuickLyric_Brown,
                R.style.Theme_QuickLyric_Dark };
        int themeNum = Integer.valueOf(sharedPref.getString("pref_theme", "0"));

        TypedValue primaryColorValue = new TypedValue();
        context.setTheme(themes[themeNum]);
        context.getTheme().resolveAttribute(R.attr.colorPrimary, primaryColorValue, true);

        notifBuilder.setSmallIcon(R.drawable.ic_notif).setContentTitle(context.getString(R.string.app_name))
                .setContentText(String.format("%s - %s", artist, track)).setContentIntent(openAppPending)
                .setVisibility(-1) // Notification.VISIBILITY_SECRET
                .setGroup("Lyrics_Notification").setColor(primaryColorValue.data).setGroupSummary(true);

        wearableNotifBuilder.setSmallIcon(R.drawable.ic_notif)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(String.format("%s - %s", artist, track)).setContentIntent(openAppPending)
                .setVisibility(-1) // Notification.VISIBILITY_SECRET
                .setGroup("Lyrics_Notification").setOngoing(false).setColor(primaryColorValue.data)
                .setGroupSummary(false)
                .extend(new NotificationCompat.WearableExtender().addAction(wearableAction));

        if (notificationPref == 2) {
            notifBuilder.setOngoing(true).setPriority(-2); // Notification.PRIORITY_MIN
            wearableNotifBuilder.setPriority(-2);
        } else
            notifBuilder.setPriority(-1); // Notification.PRIORITY_LOW

        Notification notif = notifBuilder.build();
        Notification wearableNotif = wearableNotifBuilder.build();

        if (notificationPref == 2)
            notif.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
        else
            notif.flags |= Notification.FLAG_AUTO_CANCEL;

        NotificationManagerCompat.from(context).notify(0, notif);
        try {
            context.getPackageManager().getPackageInfo("com.google.android.wearable.app",
                    PackageManager.GET_META_DATA);
            NotificationManagerCompat.from(context).notify(8, wearableNotif);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    } else if (track.equals(current.getString("track", "")))
        NotificationManagerCompat.from(context).cancel(0);
}