Search in sources :

Example 1 with Themer

use of com.winsonchiu.reader.theme.Themer in project Reader by TheKeeperOfPie.

the class ActivityLogin method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    CustomApplication.getComponentMain().inject(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    layoutRoot = (ViewGroup) findViewById(R.id.layout_root);
    Themer themer = new Themer(this);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    progressAuth = (ProgressBar) findViewById(R.id.progress_auth);
    webAuth = (WebView) findViewById(R.id.web_auth);
    webAuth.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d(TAG, "onPageStarted() called with: " + "view = [" + view + "], url = [" + url + "], favicon = [" + favicon + "]");
            progressAuth.setIndeterminate(true);
            progressAuth.setVisibility(View.VISIBLE);
            Uri uri = Uri.parse(url);
            if (uri.getHost().equals(Reddit.REDIRECT_URI.replaceFirst("https://", ""))) {
                String error = uri.getQueryParameter("error");
                String returnedState = uri.getQueryParameter("state");
                if (!TextUtils.isEmpty(error) || !state.equals(returnedState)) {
                    Toast.makeText(ActivityLogin.this, error, Toast.LENGTH_LONG).show();
                    webAuth.loadUrl(Reddit.getUserAuthUrl(state));
                    return;
                }
                // TODO: Failsafe with error and state
                String code = uri.getQueryParameter("code");
                fetchTokens(code);
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressAuth.setVisibility(View.GONE);
            toolbar.setTitle(view.getTitle());
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.e(TAG, "WebView error: " + error);
        }
    });
    webAuth.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressAuth.setIndeterminate(false);
            progressAuth.setProgress(newProgress);
        }
    });
    webAuth.loadUrl(Reddit.getUserAuthUrl(state));
}
Also used : Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) Themer(com.winsonchiu.reader.theme.Themer) WebView(android.webkit.WebView) Uri(android.net.Uri) WebViewClient(android.webkit.WebViewClient)

Example 2 with Themer

use of com.winsonchiu.reader.theme.Themer in project Reader by TheKeeperOfPie.

the class FragmentBase method onAttach.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    themer = new Themer(context);
    if (!injected) {
        inject();
        injected = true;
    }
}
Also used : Themer(com.winsonchiu.reader.theme.Themer)

Example 3 with Themer

use of com.winsonchiu.reader.theme.Themer in project Reader by TheKeeperOfPie.

the class ActivityMain method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    componentActivity = (ComponentActivity) getLastCustomNonConfigurationInstance();
    if (componentActivity == null) {
        componentActivity = CustomApplication.getComponentMain().componentActivity();
    }
    componentActivity.inject(this);
    themer = new Themer(this);
    int colorResourcePrimary = UtilsColor.showOnWhite(themer.getColorPrimary()) ? R.color.darkThemeIconFilter : R.color.lightThemeIconFilter;
    int resourceIcon = UtilsColor.showOnWhite(themer.getColorPrimary()) ? R.mipmap.app_icon_white_outline : R.mipmap.app_icon_dark_outline;
    colorFilterPrimary = new CustomColorFilter(ContextCompat.getColor(this, colorResourcePrimary), PorterDuff.Mode.MULTIPLY);
    /**
         * Required for {@link YouTubePlayerSupportFragment} to inflate proper UI
         */
    getLayoutInflater().setFactory(this);
    super.onCreate(savedInstanceState);
    handler = new Handler();
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    inflateNavigationDrawer();
    Receiver.setAlarm(this);
    layoutDrawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {

        @Override
        public void onDrawerClosed(View drawerView) {
            if (loadId != 0) {
                selectNavigationItem(loadId, null, true);
            }
            setAccountsVisible(false);
        }
    });
    eventListenerBase = new EventListenerBase(componentActivity) {

        @Override
        public void onClickComments(Link link, AdapterLink.ViewHolderLink viewHolderLink, Source source) {
            int color = viewHolderLink.getBackgroundColor();
            FragmentBase fragment = (FragmentBase) getSupportFragmentManager().findFragmentById(R.id.frame_fragment);
            fragment.onWindowTransitionStart();
            FragmentComments fragmentComments = FragmentComments.newInstance(viewHolderLink, color);
            fragmentComments.setFragmentToHide(fragment, viewHolderLink.itemView);
            getSupportFragmentManager().beginTransaction().add(R.id.frame_fragment, fragmentComments, FragmentComments.TAG).addToBackStack(null).commit();
            controllerCommentsTop.setLink(link, source);
        }

        @Override
        public void loadUrl(String urlString) {
            if (sharedPreferences.getBoolean(AppSettings.PREF_EXTERNAL_BROWSER, false)) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(urlString));
                startActivity(intent);
                return;
            }
            Log.d(TAG, "loadUrl: " + loadId);
            URL url = null;
            try {
                url = new URL(urlString);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            if (url != null && url.getHost().contains("reddit.com")) {
                Intent intent = new Intent(ActivityMain.this, ActivityMain.class);
                intent.setAction(Intent.ACTION_VIEW);
                intent.putExtra(REDDIT_PAGE, urlString);
                startActivity(intent);
                return;
            }
            launchUrl(urlString, false);
        }

        @Override
        public void downloadImage(String title, String fileName, String url) {
            imageDownload = new ImageDownload(title, fileName, url);
            ActivityCompat.requestPermissions(ActivityMain.this, new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
        }

        @Override
        public void editLink(Link link) {
            FragmentNewPost fragmentNewPost = FragmentNewPost.newInstanceEdit(controllerUser.getUser().getName(), link);
            getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.frame_fragment)).add(R.id.frame_fragment, fragmentNewPost, FragmentNewPost.TAG).addToBackStack(null).commit();
        }

        @Override
        public void showReplyEditor(Replyable replyable) {
            FragmentReply fragmentReply = FragmentReply.newInstance(replyable);
            fragmentReply.setFragmentToHide(getSupportFragmentManager().findFragmentById(R.id.frame_fragment));
            getSupportFragmentManager().beginTransaction().add(R.id.frame_fragment, fragmentReply, FragmentReply.TAG).addToBackStack(null).commit();
        }

        @Override
        public void loadWebFragment(String url) {
            launchUrl(url, false);
        }

        @Override
        public void launchScreen(Intent intent) {
            startActivity(intent);
        }
    };
    eventListenerComment = (comment, subreddit, linkId) -> {
        if (comment.getCount() == 0) {
            Intent intentCommentThread = new Intent(ActivityMain.this, ActivityMain.class);
            intentCommentThread.setAction(Intent.ACTION_VIEW);
            // Double slashes used to trigger parseUrl correctly
            intentCommentThread.putExtra(REDDIT_PAGE, Reddit.BASE_URL + "/r/" + subreddit + "/comments/" + linkId + "/title/" + comment.getParentId() + "/");
            startActivity(intentCommentThread);
            return true;
        }
        return false;
    };
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription("Reader", BitmapFactory.decodeResource(getResources(), resourceIcon), themer.getColorPrimary());
        setTaskDescription(taskDescription);
    }
    customTabsServiceConnection = new CustomTabsServiceConnection() {

        @Override
        public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) {
            customTabsClient.warmup(0);
            customTabsSession = customTabsClient.newSession(new CustomTabsCallback() {

                @Override
                public void onNavigationEvent(int navigationEvent, Bundle extras) {
                    super.onNavigationEvent(navigationEvent, extras);
                    Log.d(TAG, "onNavigationEvent() called with: " + "navigationEvent = [" + navigationEvent + "], extras = [" + extras + "]");
                }

                @Override
                public void extraCallback(String callbackName, Bundle args) {
                    super.extraCallback(callbackName, args);
                    Log.d(TAG, "extraCallback() called with: " + "callbackName = [" + callbackName + "], args = [" + args + "]");
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    CustomTabsClient.bindCustomTabsService(this, getPackageName(), customTabsServiceConnection);
    handleFirstLaunch(savedInstanceState);
    showBetaNotice();
    loadAccount();
    UtilsImage.checkMaxTextureSize(handler, () -> {
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) AdapterLink(com.winsonchiu.reader.links.AdapterLink) ImageDownload(com.winsonchiu.reader.utils.ImageDownload) ActivityManager(android.app.ActivityManager) Source(com.winsonchiu.reader.comments.Source) URL(java.net.URL) CustomTabsCallback(android.support.customtabs.CustomTabsCallback) ComponentName(android.content.ComponentName) DrawerLayout(android.support.v4.widget.DrawerLayout) CustomTabsClient(android.support.customtabs.CustomTabsClient) CustomColorFilter(com.winsonchiu.reader.utils.CustomColorFilter) Bundle(android.os.Bundle) Themer(com.winsonchiu.reader.theme.Themer) Handler(android.os.Handler) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) FragmentReply(com.winsonchiu.reader.comments.FragmentReply) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) RecyclerView(android.support.v7.widget.RecyclerView) NavigationView(android.support.design.widget.NavigationView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) EventListenerBase(com.winsonchiu.reader.utils.EventListenerBase) Replyable(com.winsonchiu.reader.data.reddit.Replyable) CustomTabsServiceConnection(android.support.customtabs.CustomTabsServiceConnection) Link(com.winsonchiu.reader.data.reddit.Link) AdapterLink(com.winsonchiu.reader.links.AdapterLink) FragmentComments(com.winsonchiu.reader.comments.FragmentComments)

Example 4 with Themer

use of com.winsonchiu.reader.theme.Themer in project Reader by TheKeeperOfPie.

the class Receiver method checkInbox.

public void checkInbox(final Context context, @Nullable final ArrayList<String> names) {
    final ArrayList<String> readNames;
    if (names == null) {
        readNames = new ArrayList<>();
    } else {
        readNames = names;
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Listing messages = new Listing();
            Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
            for (Account account : accounts) {
                final AccountManagerFuture<Bundle> futureAuth = accountManager.getAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, null, true, null, null);
                try {
                    Bundle bundle = futureAuth.getResult();
                    final String tokenAuth = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/message/unread").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
                    String response = okHttpClient.newCall(request).execute().body().string();
                    Listing listing = Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class));
                    messages.addChildren(listing.getChildren());
                    Log.d(TAG, account.name + " checkInbox response: " + response);
                } catch (OperationCanceledException | AuthenticatorException | IOException e) {
                    e.printStackTrace();
                }
            }
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            Thing thing = null;
            for (int index = 0; index < messages.getChildren().size(); index++) {
                thing = messages.getChildren().get(index);
                if (readNames.contains(thing.getName())) {
                    reddit.markRead(thing.getName()).subscribe(new ObserverEmpty<>());
                    thing = null;
                } else {
                    readNames.add(thing.getName());
                    break;
                }
            }
            if (thing == null) {
                notificationManager.cancel(NOTIFICATION_INBOX);
                return;
            }
            int titleSuffixResource = messages.getChildren().size() == 1 ? R.string.new_message : R.string.new_messages;
            CharSequence content = "";
            CharSequence author = "";
            CharSequence dest = "";
            if (thing instanceof Message) {
                content = ((Message) thing).getBodyHtml();
                author = ((Message) thing).getAuthor();
                dest = ((Message) thing).getDest();
            } else if (thing instanceof Comment) {
                content = ((Comment) thing).getBodyHtml();
                author = ((Comment) thing).getAuthor();
                dest = ((Comment) thing).getDest();
            }
            Intent intentActivity = new Intent(context, ActivityMain.class);
            intentActivity.putExtra(ActivityMain.ACCOUNT, dest);
            intentActivity.putExtra(ActivityMain.NAV_ID, R.id.item_inbox);
            intentActivity.putExtra(ActivityMain.NAV_PAGE, ControllerInbox.UNREAD);
            PendingIntent pendingIntentActivity = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_CANCEL_CURRENT);
            Intent intentRecheckInbox = new Intent(INTENT_INBOX);
            intentRecheckInbox.putExtra(READ_NAMES, readNames);
            PendingIntent pendingIntentRecheckInbox = PendingIntent.getBroadcast(context, 0, intentRecheckInbox, PendingIntent.FLAG_CANCEL_CURRENT);
            Themer themer = new Themer(context);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.app_icon_white_outline).setContentTitle(messages.getChildren().size() + " " + context.getResources().getString(titleSuffixResource)).setContentText(context.getString(R.string.expand_to_read_first_message)).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(context.getString(R.string.from) + " /u/" + author).bigText(content)).setContentIntent(pendingIntentActivity).addAction(new NotificationCompat.Action(R.drawable.ic_check_white_24dp, context.getString(R.string.mark_read), pendingIntentRecheckInbox)).setDeleteIntent(pendingIntentRecheckInbox).setAutoCancel(true).setCategory(NotificationCompat.CATEGORY_EMAIL).setColor(themer.getColorPrimary()).setLights(themer.getColorPrimary(), LED_MS_ON, LED_MS_OFF);
            notificationManager.notify(NOTIFICATION_INBOX, builder.build());
        }
    }).start();
}
Also used : Account(android.accounts.Account) Message(com.winsonchiu.reader.data.reddit.Message) OperationCanceledException(android.accounts.OperationCanceledException) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) JsonNode(com.fasterxml.jackson.databind.JsonNode) NotificationCompat(android.support.v4.app.NotificationCompat) Thing(com.winsonchiu.reader.data.reddit.Thing) Comment(com.winsonchiu.reader.data.reddit.Comment) Bundle(android.os.Bundle) Request(okhttp3.Request) AuthenticatorException(android.accounts.AuthenticatorException) Themer(com.winsonchiu.reader.theme.Themer) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) Listing(com.winsonchiu.reader.data.reddit.Listing) PendingIntent(android.app.PendingIntent)

Example 5 with Themer

use of com.winsonchiu.reader.theme.Themer in project Reader by TheKeeperOfPie.

the class ActivitySettings method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    Themer themer = new Themer(this);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(R.string.settings);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().replace(R.id.frame_fragment, FragmentHeaders.newInstance(), FragmentHeaders.TAG).commit();
    } else if (getFragmentManager().getBackStackEntryCount() > 0) {
        Fragment fragment = getFragmentManager().findFragmentByTag(FragmentHeaders.TAG);
        if (fragment != null) {
            getFragmentManager().beginTransaction().hide(fragment).commit();
        }
        setResult(Activity.RESULT_OK);
    }
}
Also used : Themer(com.winsonchiu.reader.theme.Themer) View(android.view.View) Fragment(android.app.Fragment)

Aggregations

Themer (com.winsonchiu.reader.theme.Themer)5 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 View (android.view.View)2 Account (android.accounts.Account)1 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 ActivityManager (android.app.ActivityManager)1 Fragment (android.app.Fragment)1 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 CustomTabsCallback (android.support.customtabs.CustomTabsCallback)1 CustomTabsClient (android.support.customtabs.CustomTabsClient)1 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)1 CustomTabsServiceConnection (android.support.customtabs.CustomTabsServiceConnection)1 NavigationView (android.support.design.widget.NavigationView)1 NotificationCompat (android.support.v4.app.NotificationCompat)1