List of usage examples for android.content.pm PackageManager resolveActivity
public abstract ResolveInfo resolveActivity(Intent intent, @ResolveInfoFlags int flags);
From source file:net.gsantner.opoc.util.ShareUtil.java
/** * By default Chrome Custom Tabs only uses Chrome Stable to open links * There are also other packages (like Chrome Beta, Chromium, Firefox, ..) * which implement the Chrome Custom Tab interface. This method changes * the customtab intent to use an available compatible browser, if available. *///from ww w. j av a2 s . c o m public void enableChromeCustomTabsForOtherBrowsers(Intent customTabIntent) { String[] checkpkgs = new String[] { "com.android.chrome", "com.chrome.beta", "com.chrome.dev", "com.google.android.apps.chrome", "org.chromium.chrome", "org.mozilla.fennec_fdroid", "org.mozilla.firefox", "org.mozilla.firefox_beta", "org.mozilla.fennec_aurora", "org.mozilla.klar", "org.mozilla.focus", }; // Get all intent handlers for web links PackageManager pm = _context.getPackageManager(); Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com")); List<String> browsers = new ArrayList<>(); for (ResolveInfo ri : pm.queryIntentActivities(urlIntent, 0)) { Intent i = new Intent("android.support.customtabs.action.CustomTabsService"); i.setPackage(ri.activityInfo.packageName); if (pm.resolveService(i, 0) != null) { browsers.add(ri.activityInfo.packageName); } } // Check if the user has a "default browser" selected ResolveInfo ri = pm.resolveActivity(urlIntent, 0); String userDefaultBrowser = (ri == null) ? null : ri.activityInfo.packageName; // Select which browser to use out of all installed customtab supporting browsers String pkg = null; if (browsers.isEmpty()) { pkg = null; } else if (browsers.size() == 1) { pkg = browsers.get(0); } else if (!TextUtils.isEmpty(userDefaultBrowser) && browsers.contains(userDefaultBrowser)) { pkg = userDefaultBrowser; } else { for (String checkpkg : checkpkgs) { if (browsers.contains(checkpkg)) { pkg = checkpkg; break; } } if (pkg == null && !browsers.isEmpty()) { pkg = browsers.get(0); } } if (pkg != null && customTabIntent != null) { customTabIntent.setPackage(pkg); } }
From source file:com.kncwallet.wallet.ui.WalletActivity.java
private void timeskewAlert(final long diffMinutes) { final PackageManager pm = getPackageManager(); final Intent settingsIntent = new Intent(android.provider.Settings.ACTION_DATE_SETTINGS); final KnCDialog.Builder builder = new KnCDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.wallet_timeskew_dialog_title); builder.setMessage(getString(R.string.wallet_timeskew_dialog_msg, diffMinutes)); if (pm.resolveActivity(settingsIntent, 0) != null) { builder.setPositiveButton(R.string.wallet_timeskew_dialog_button_settings, new DialogInterface.OnClickListener() { @Override//from w w w . j a va 2 s. c o m public void onClick(final DialogInterface dialog, final int id) { startActivity(settingsIntent); finish(); } }); } builder.setNegativeButton(R.string.button_dismiss, null); builder.show(); }
From source file:net.ddns.mlsoftlaberge.contactslist.ui.ContactAdminFragment.java
public void openaddress(String address) { final Intent viewIntent = new Intent(Intent.ACTION_VIEW, constructGeoUri(address)); // A PackageManager instance is needed to verify that there's a default app // that handles ACTION_VIEW and a geo Uri. final PackageManager packageManager = getActivity().getPackageManager(); // Checks for an activity that can handle this intent. Preferred in this // case over Intent.createChooser() as it will still let the user choose // a default (or use a previously set default) for geo Uris. if (packageManager.resolveActivity(viewIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) { startActivity(viewIntent);/*from w w w .j a v a2 s .c o m*/ } else { // If no default is found, displays a message that no activity can handle // the view button. Toast.makeText(getActivity(), R.string.no_intent_found, Toast.LENGTH_SHORT).show(); } }
From source file:com.hivewallet.androidclient.wallet.ui.WalletActivity.java
private Dialog createVersionAlertDialog() { final PackageManager pm = getPackageManager(); final Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Constants.MARKET_APP_URL, getPackageName()))); final Intent binaryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.BINARY_URL)); final DialogBuilder dialog = DialogBuilder.warn(this, R.string.wallet_version_dialog_title); final StringBuilder message = new StringBuilder(getString(R.string.wallet_version_dialog_msg)); if (Build.VERSION.SDK_INT < Constants.SDK_DEPRECATED_BELOW) message.append("\n\n").append(getString(R.string.wallet_version_dialog_msg_deprecated)); dialog.setMessage(message);/*from w w w. j a va 2 s.c o m*/ if (pm.resolveActivity(marketIntent, 0) != null) { dialog.setPositiveButton(R.string.wallet_version_dialog_button_market, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int id) { startActivity(marketIntent); finish(); } }); } if (pm.resolveActivity(binaryIntent, 0) != null) { dialog.setNeutralButton(R.string.wallet_version_dialog_button_binary, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int id) { startActivity(binaryIntent); finish(); } }); } dialog.setNegativeButton(R.string.button_dismiss, null); return dialog.create(); }
From source file:net.ddns.mlsoftlaberge.contactslist.ui.ContactAdminFragment.java
/** * Builds an address LinearLayout based on address information from the Contacts Provider. * Each address for the contact gets its own LinearLayout object; for example, if the contact * has three postal addresses, then 3 LinearLayouts are generated. * * @param addressType From/* ww w .j a v a2s. co m*/ * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#TYPE} * @param addressTypeLabel From * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#LABEL} * @param address From * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#FORMATTED_ADDRESS} * @return A LinearLayout to add to the contact details layout, * populated with the provided address details. */ private LinearLayout buildAddressLayout(int addressType, String addressTypeLabel, final String address) { // Inflates the address layout final LinearLayout addressLayout = (LinearLayout) LayoutInflater.from(getActivity()) .inflate(R.layout.contact_address_item, mAddressLayout, false); // Gets handles to the view objects in the layout final TextView headerTextView = (TextView) addressLayout.findViewById(R.id.contact_address_header); final TextView addressTextView = (TextView) addressLayout.findViewById(R.id.contact_address_full); final ImageButton viewAddressButton = (ImageButton) addressLayout.findViewById(R.id.button_view_address); // If there's no addresses for the contact, shows the empty view and message, and hides the // header and button. if (addressTypeLabel == null && addressType == 0) { headerTextView.setVisibility(View.GONE); viewAddressButton.setVisibility(View.GONE); addressTextView.setText(R.string.no_address); } else { // Gets postal address label type CharSequence label = StructuredPostal.getTypeLabel(getResources(), addressType, addressTypeLabel); // Sets TextView objects in the layout headerTextView.setText(label + " Address"); addressTextView.setText(address); viewAddressButton.setContentDescription(address); // Defines an onClickListener object for the address button viewAddressButton.setOnClickListener(new View.OnClickListener() { // Defines what to do when users click the address button @Override public void onClick(View view) { final Intent viewIntent = new Intent(Intent.ACTION_VIEW, constructGeoUri(view.getContentDescription().toString())); // A PackageManager instance is needed to verify that there's a default app // that handles ACTION_VIEW and a geo Uri. final PackageManager packageManager = getActivity().getPackageManager(); // Checks for an activity that can handle this intent. Preferred in this // case over Intent.createChooser() as it will still let the user choose // a default (or use a previously set default) for geo Uris. if (packageManager.resolveActivity(viewIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) { // Toast.makeText(getActivity(), // R.string.yes_intent_found, Toast.LENGTH_SHORT).show(); startActivity(viewIntent); } else { // If no default is found, displays a message that no activity can handle // the view button. Toast.makeText(getActivity(), R.string.no_intent_found, Toast.LENGTH_SHORT).show(); } } }); } return addressLayout; }
From source file:net.ddns.mlsoftlaberge.trycorder.contacts.ContactAdminFragment.java
/** * Builds an address LinearLayout based on address information from the Contacts Provider. * Each address for the contact gets its own LinearLayout object; for example, if the contact * has three postal addresses, then 3 LinearLayouts are generated. * * @param addressType From/* ww w. j a v a 2 s. c o m*/ * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#TYPE} * @param addressTypeLabel From * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#LABEL} * @param address From * {@link android.provider.ContactsContract.CommonDataKinds.StructuredPostal#FORMATTED_ADDRESS} * @return A LinearLayout to add to the contact details layout, * populated with the provided address details. */ private LinearLayout buildAddressLayout(int addressType, String addressTypeLabel, final String address) { // Inflates the address layout final LinearLayout addressLayout = (LinearLayout) LayoutInflater.from(getActivity()) .inflate(R.layout.contact_address_item, mAddressLayout, false); // Gets handles to the view objects in the layout final TextView headerTextView = (TextView) addressLayout.findViewById(R.id.contact_address_header); final TextView addressTextView = (TextView) addressLayout.findViewById(R.id.contact_address_full); final ImageButton viewAddressButton = (ImageButton) addressLayout.findViewById(R.id.button_view_address); // If there's no addresses for the contact, shows the empty view and message, and hides the // header and button. if (addressTypeLabel == null && addressType == 0) { headerTextView.setVisibility(View.GONE); viewAddressButton.setVisibility(View.GONE); addressTextView.setText(R.string.no_address); } else { // Gets postal address label type CharSequence label = StructuredPostal.getTypeLabel(getResources(), addressType, addressTypeLabel); // Sets TextView objects in the layout headerTextView.setText(label + " Address"); addressTextView.setText(address); viewAddressButton.setContentDescription(address); // Defines an onClickListener object for the address button viewAddressButton.setOnClickListener(new View.OnClickListener() { // Defines what to do when users click the address button @Override public void onClick(View view) { buttonsound(); final Intent viewIntent = new Intent(Intent.ACTION_VIEW, constructGeoUri(view.getContentDescription().toString())); // A PackageManager instance is needed to verify that there's a default app // that handles ACTION_VIEW and a geo Uri. final PackageManager packageManager = getActivity().getPackageManager(); // Checks for an activity that can handle this intent. Preferred in this // case over Intent.createChooser() as it will still let the user choose // a default (or use a previously set default) for geo Uris. if (packageManager.resolveActivity(viewIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) { // Toast.makeText(getActivity(), // R.string.yes_intent_found, Toast.LENGTH_SHORT).show(); startActivity(viewIntent); } else { // If no default is found, displays a message that no activity can handle // the view button. Toast.makeText(getActivity(), R.string.no_intent_found, Toast.LENGTH_SHORT).show(); } } }); } return addressLayout; }
From source file:com.kncwallet.wallet.ui.WalletActivity.java
private void versionAlert(final int serverVersionCode) { final PackageManager pm = getPackageManager(); final Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Constants.MARKET_APP_URL, getPackageName()))); final Intent binaryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.BINARY_URL)); final KnCDialog.Builder builder = new KnCDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.wallet_version_dialog_title); builder.setMessage(getString(R.string.wallet_version_dialog_msg)); if (pm.resolveActivity(marketIntent, 0) != null) { builder.setPositiveButton(R.string.wallet_version_dialog_button_market, new DialogInterface.OnClickListener() { @Override/*from w w w. j a v a 2 s . co m*/ public void onClick(final DialogInterface dialog, final int id) { startActivity(marketIntent); finish(); } }); } if (pm.resolveActivity(binaryIntent, 0) != null) { builder.setNeutralButton(R.string.wallet_version_dialog_button_binary, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int id) { startActivity(binaryIntent); finish(); } }); } builder.setNegativeButton(R.string.button_dismiss, null); builder.show(); }
From source file:com.filemanager.free.utils.Futils.java
public void createVersionAlertDialog(final MainActivity context) { final PackageManager pm = context.getPackageManager(); final Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Util.MARKET_APP_URL, context.getPackageName()))); MaterialDialog.Builder a = new MaterialDialog.Builder(context); if (context.theme1 == 1) a.theme(Theme.DARK);/*from w w w . j a v a2 s. c o m*/ a.positiveText(R.string.wallet_version_dialog_button_market) .positiveColor(Color.parseColor(context.fabskin)); a.negativeText(R.string.button_dismiss).negativeColor(Color.parseColor(context.fabskin)); if (pm.resolveActivity(marketIntent, 0) != null) { a.onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { context.startActivity(marketIntent); dialog.dismiss(); } }); } a.onNeutral(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog.dismiss(); } }); final StringBuilder message = new StringBuilder(getString(context, R.string.wallet_version_dialog_msg)); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) message.append("\n\n").append(getString(context, R.string.wallet_version_dialog_msg_deprecated)); a.title(R.string.wallet_version_dialog_title); a.content(message); a.build().show(); }
From source file:com.master.metehan.filtereagle.AdapterRule.java
@Override public void onBindViewHolder(final ViewHolder holder, final int position) { // Get rule/*from w w w . j a v a 2 s.co m*/ final Rule rule = listFiltered.get(position); // Handle expanding/collapsing holder.llApplication.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { rule.expanded = !rule.expanded; notifyItemChanged(position); } }); // Show if non default rules holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT); // Show expand/collapse indicator holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0); // Show application icon if (rule.info.applicationInfo == null || rule.info.applicationInfo.icon == 0) Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(holder.ivIcon); else { Uri uri = Uri .parse("android.resource://" + rule.info.packageName + "/" + rule.info.applicationInfo.icon); Picasso.with(context).load(uri).resize(iconSize, iconSize).into(holder.ivIcon); } // Show application label holder.tvName.setText(rule.name); holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE); holder.tvDescription.setText(rule.description); // Show application state int color = rule.system ? colorOff : colorText; if (!rule.internet || !rule.enabled) color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color)); holder.tvName.setTextColor(color); // Show rule count new AsyncTask<Object, Object, Long>() { @Override protected void onPreExecute() { holder.tvHosts.setVisibility(View.GONE); } @Override protected Long doInBackground(Object... objects) { return DatabaseHelper.getInstance(context).getRuleCount(rule.info.applicationInfo.uid); } @Override protected void onPostExecute(Long rules) { if (rules > 0) { holder.tvHosts.setVisibility(View.VISIBLE); holder.tvHosts.setText(Long.toString(rules)); } } }.execute(); // Wi-Fi settings holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f); holder.cbWifi.setOnCheckedChangeListener(null); holder.cbWifi.setChecked(rule.wifi_blocked); holder.cbWifi.setEnabled(rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbWifi)); DrawableCompat.setTint(wrap, rule.apply ? (rule.wifi_blocked ? colorOff : colorOn) : colorGrayed); } holder.cbWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.wifi_blocked = isChecked; updateRule(rule, true, listAll); } }); holder.ivScreenWifi.setEnabled(rule.apply); holder.ivScreenWifi.setAlpha(wifiActive ? 1 : 0.5f); holder.ivScreenWifi.setVisibility(rule.screen_wifi && rule.wifi_blocked ? View.VISIBLE : View.INVISIBLE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivScreenWifi.getDrawable()); DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed); } // Mobile settings holder.cbOther.setAlpha(otherActive ? 1 : 0.5f); holder.cbOther.setOnCheckedChangeListener(null); holder.cbOther.setChecked(rule.other_blocked); holder.cbOther.setEnabled(rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbOther)); DrawableCompat.setTint(wrap, rule.apply ? (rule.other_blocked ? colorOff : colorOn) : colorGrayed); } holder.cbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.other_blocked = isChecked; updateRule(rule, true, listAll); } }); holder.ivScreenOther.setEnabled(rule.apply); holder.ivScreenOther.setAlpha(otherActive ? 1 : 0.5f); holder.ivScreenOther.setVisibility(rule.screen_other && rule.other_blocked ? View.VISIBLE : View.INVISIBLE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivScreenOther.getDrawable()); DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed); } holder.tvRoaming.setTextColor(rule.apply ? colorOff : colorGrayed); holder.tvRoaming.setAlpha(otherActive ? 1 : 0.5f); holder.tvRoaming.setVisibility( rule.roaming && (!rule.other_blocked || rule.screen_other) ? View.VISIBLE : View.INVISIBLE); // Expanded configuration section holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE); // Show application details holder.tvUid .setText(rule.info.applicationInfo == null ? "?" : Integer.toString(rule.info.applicationInfo.uid)); holder.tvPackage.setText(rule.info.packageName); holder.tvVersion.setText(rule.info.versionName + '/' + rule.info.versionCode); // Show application state holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE); holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE); // Apply holder.cbApply .setVisibility(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? View.GONE : View.VISIBLE); holder.cbApply.setEnabled(rule.pkg); holder.cbApply.setOnCheckedChangeListener(null); holder.cbApply.setChecked(rule.apply); holder.cbApply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.apply = isChecked; updateRule(rule, true, listAll); } }); // Show related holder.btnRelated.setVisibility(rule.relateduids ? View.VISIBLE : View.GONE); holder.btnRelated.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent main = new Intent(context, ActivityMain.class); main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.info.applicationInfo.uid)); context.startActivity(main); } }); // Launch application settings final Intent settings = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); settings.setData(Uri.parse("package:" + rule.info.packageName)); holder.ibSettings.setVisibility( settings.resolveActivity(context.getPackageManager()) == null ? View.GONE : View.VISIBLE); holder.ibSettings.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(settings); } }); // Launch application holder.ibLaunch.setVisibility(rule.intent == null ? View.GONE : View.VISIBLE); holder.ibLaunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(rule.intent); } }); // Show Wi-Fi screen on condition holder.cbScreenWifi.setOnCheckedChangeListener(null); holder.cbScreenWifi.setChecked(rule.screen_wifi); holder.cbScreenWifi.setEnabled(rule.wifi_blocked && rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivWifiLegend.getDrawable()); DrawableCompat.setTint(wrap, colorOn); } holder.cbScreenWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.screen_wifi = isChecked; updateRule(rule, true, listAll); } }); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivOtherLegend.getDrawable()); DrawableCompat.setTint(wrap, colorOn); } // Show mobile screen on condition holder.cbScreenOther.setOnCheckedChangeListener(null); holder.cbScreenOther.setChecked(rule.screen_other); holder.cbScreenOther.setEnabled(rule.other_blocked && rule.apply); holder.cbScreenOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.screen_other = isChecked; updateRule(rule, true, listAll); } }); // Show roaming condition holder.cbRoaming.setOnCheckedChangeListener(null); holder.cbRoaming.setChecked(rule.roaming); holder.cbRoaming.setEnabled((!rule.other_blocked || rule.screen_other) && rule.apply); holder.cbRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @TargetApi(Build.VERSION_CODES.M) public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.roaming = isChecked; updateRule(rule, true, listAll); // Request permissions if (isChecked && !Util.hasPhoneStatePermission(context)) context.requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE }, ActivityMain.REQUEST_ROAMING); } }); // Reset rule holder.btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Util.areYouSure(view.getContext(), R.string.msg_clear_rules, new Util.DoubtListener() { @Override public void onSure() { holder.cbApply.setChecked(true); holder.cbWifi.setChecked(rule.wifi_default); holder.cbOther.setChecked(rule.other_default); holder.cbScreenWifi.setChecked(rule.screen_wifi_default); holder.cbScreenOther.setChecked(rule.screen_other_default); holder.cbRoaming.setChecked(rule.roaming_default); } }); } }); // Show access rules if (rule.expanded) { // Access the database when expanded only final AdapterAccess badapter = new AdapterAccess(context, DatabaseHelper.getInstance(context).getAccess(rule.info.applicationInfo.uid)); if (filter) holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) { PackageManager pm = context.getPackageManager(); Cursor cursor = (Cursor) badapter.getItem(bposition); final long id = cursor.getLong(cursor.getColumnIndex("ID")); int version = cursor.getInt(cursor.getColumnIndex("version")); int protocol = cursor.getInt(cursor.getColumnIndex("protocol")); String daddr = cursor.getString(cursor.getColumnIndex("daddr")); int dport = cursor.getInt(cursor.getColumnIndex("dport")); long time = cursor.getLong(cursor.getColumnIndex("time")); int block = cursor.getInt(cursor.getColumnIndex("block")); PopupMenu popup = new PopupMenu(context, context.findViewById(R.id.vwPopupAnchor)); popup.inflate(R.menu.access); popup.getMenu().findItem(R.id.menu_host) .setTitle(Util.getProtocolName(protocol, version, false) + " " + daddr + (dport > 0 ? "/" + dport : "")); // Whois final Intent lookupIP = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.tcpiputils.com/whois-lookup/" + daddr)); if (pm.resolveActivity(lookupIP, 0) == null) popup.getMenu().removeItem(R.id.menu_whois); else popup.getMenu().findItem(R.id.menu_whois) .setTitle(context.getString(R.string.title_log_whois, daddr)); // Lookup port final Intent lookupPort = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.speedguide.net/port.php?port=" + dport)); if (dport <= 0 || pm.resolveActivity(lookupPort, 0) == null) popup.getMenu().removeItem(R.id.menu_port); else popup.getMenu().findItem(R.id.menu_port) .setTitle(context.getString(R.string.title_log_port, dport)); popup.getMenu().findItem(R.id.menu_time) .setTitle(SimpleDateFormat.getDateTimeInstance().format(time)); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_whois: context.startActivity(lookupIP); return true; case R.id.menu_port: context.startActivity(lookupPort); return true; case R.id.menu_allow: if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) { DatabaseHelper.getInstance(context).setAccess(id, 0); ServiceSinkhole.reload("allow host", context); } else context.startActivity(new Intent(context, ActivityPro.class)); return true; case R.id.menu_block: if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) { DatabaseHelper.getInstance(context).setAccess(id, 1); ServiceSinkhole.reload("block host", context); } else context.startActivity(new Intent(context, ActivityPro.class)); return true; case R.id.menu_reset: DatabaseHelper.getInstance(context).setAccess(id, -1); ServiceSinkhole.reload("reset host", context); return true; } return false; } }); if (block == 0) popup.getMenu().removeItem(R.id.menu_allow); else if (block == 1) popup.getMenu().removeItem(R.id.menu_block); popup.show(); } }); else holder.lvAccess.setOnItemClickListener(null); holder.lvAccess.setAdapter(badapter); } else { holder.lvAccess.setAdapter(null); holder.lvAccess.setOnItemClickListener(null); } // Show logging is disabled final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean log_app = prefs.getBoolean("log_app", false); holder.tvNolog.setVisibility(log_app ? View.GONE : View.VISIBLE); holder.tvNolog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(new Intent(context, ActivitySettings.class)); } }); // Show disable access notifications setting holder.cbNotify.setOnCheckedChangeListener(null); holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply); holder.cbNotify.setChecked(rule.notify); holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.notify = isChecked; updateRule(rule, true, listAll); } }); // Clear access log holder.btnClearAccess.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Util.areYouSure(view.getContext(), R.string.msg_reset_access, new Util.DoubtListener() { @Override public void onSure() { DatabaseHelper.getInstance(context).clearAccess(rule.info.applicationInfo.uid, true); if (rv != null) rv.scrollToPosition(position); } }); } }); // Show traffic statistics holder.tvStatistics.setText(context.getString(R.string.msg_mbday, rule.upspeed, rule.downspeed)); }
From source file:com.zhengde163.netguard.AdapterRule.java
@Override public void onBindViewHolder(final ViewHolder holder, final int position) { // Get rule/*from w w w .j a v a2 s.c om*/ final Rule rule = listFiltered.get(position); // Handle expanding/collapsing holder.llApplication.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { rule.expanded = !rule.expanded; notifyItemChanged(position); } }); holder.ivExpander.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { rule.expanded = !rule.expanded; notifyItemChanged(position); } }); // Show if non default rules holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT); // Show expand/collapse indicator holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0); // Show application icon if (rule.info.applicationInfo == null || rule.info.applicationInfo.icon == 0) Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(holder.ivIcon); else { Uri uri = Uri .parse("android.resource://" + rule.info.packageName + "/" + rule.info.applicationInfo.icon); Picasso.with(context).load(uri).resize(iconSize, iconSize).into(holder.ivIcon); } // Show application label holder.tvName.setText(rule.name); holder.tvDescription.setVisibility(rule.description == null ? View.GONE : View.VISIBLE); holder.tvDescription.setText(rule.description); // Show application state int color = rule.system ? colorOff : colorText; if (!rule.internet || !rule.enabled) color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color)); holder.tvName.setTextColor(color); // Show rule count new AsyncTask<Object, Object, Long>() { @Override protected void onPreExecute() { holder.tvHosts.setVisibility(View.GONE); } @Override protected Long doInBackground(Object... objects) { return DatabaseHelper.getInstance(context).getRuleCount(rule.info.applicationInfo.uid); } @Override protected void onPostExecute(Long rules) { if (rules > 0) { holder.tvHosts.setVisibility(View.VISIBLE); holder.tvHosts.setText(Long.toString(rules)); } } }.execute(); // Wi-Fi settings holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f); holder.cbWifi.setOnCheckedChangeListener(null); holder.cbWifi.setChecked(rule.wifi_blocked); holder.cbWifi.setEnabled(rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbWifi)); DrawableCompat.setTint(wrap, rule.apply ? (rule.wifi_blocked ? colorOff : colorOn) : colorGrayed); } holder.cbWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.wifi_blocked = isChecked; updateRule(rule, true, listAll); } }); holder.ivScreenWifi.setEnabled(rule.apply); holder.ivScreenWifi.setAlpha(wifiActive ? 1 : 0.5f); holder.ivScreenWifi.setVisibility(rule.screen_wifi && rule.wifi_blocked ? View.VISIBLE : View.INVISIBLE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivScreenWifi.getDrawable()); DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed); } // Mobile settings holder.cbOther.setAlpha(otherActive ? 1 : 0.5f); holder.cbOther.setOnCheckedChangeListener(null); holder.cbOther.setChecked(rule.other_blocked); holder.cbOther.setEnabled(rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbOther)); DrawableCompat.setTint(wrap, rule.apply ? (rule.other_blocked ? colorOff : colorOn) : colorGrayed); } holder.cbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.other_blocked = isChecked; updateRule(rule, true, listAll); } }); holder.ivScreenOther.setEnabled(rule.apply); holder.ivScreenOther.setAlpha(otherActive ? 1 : 0.5f); holder.ivScreenOther.setVisibility(rule.screen_other && rule.other_blocked ? View.VISIBLE : View.INVISIBLE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivScreenOther.getDrawable()); DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed); } holder.tvRoaming.setTextColor(rule.apply ? colorOff : colorGrayed); holder.tvRoaming.setAlpha(otherActive ? 1 : 0.5f); holder.tvRoaming.setVisibility( rule.roaming && (!rule.other_blocked || rule.screen_other) ? View.VISIBLE : View.INVISIBLE); // Expanded configuration section holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE); // Show application details holder.tvUid.setText(rule.info.applicationInfo == null ? "?" : "uid : " + Integer.toString(rule.info.applicationInfo.uid)); holder.tvPackage.setText("??? : " + rule.info.packageName); holder.tvVersion.setText("? : " + rule.info.versionName + '/' + rule.info.versionCode); // Show application state holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE); holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE); // Apply holder.cbApply .setVisibility(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? View.GONE : View.VISIBLE); holder.cbApply.setEnabled(rule.pkg); holder.cbApply.setOnCheckedChangeListener(null); holder.cbApply.setChecked(rule.apply); holder.cbApply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.apply = isChecked; updateRule(rule, true, listAll); } }); // Show related holder.btnRelated.setVisibility(rule.relateduids ? View.VISIBLE : View.GONE); holder.btnRelated.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent main = new Intent(context, ActivityMain.class); main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.info.applicationInfo.uid)); context.startActivity(main); } }); // Launch application settings final Intent settings = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); settings.setData(Uri.parse("package:" + rule.info.packageName)); holder.ibSettings.setVisibility( settings.resolveActivity(context.getPackageManager()) == null ? View.GONE : View.VISIBLE); holder.ibSettings.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(settings); } }); // Launch application holder.ibLaunch.setVisibility(rule.intent == null ? View.GONE : View.VISIBLE); holder.ibLaunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(rule.intent); } }); // Show Wi-Fi screen on condition holder.cbScreenWifi.setOnCheckedChangeListener(null); holder.cbScreenWifi.setChecked(rule.screen_wifi); holder.cbScreenWifi.setEnabled(rule.wifi_blocked && rule.apply); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivWifiLegend.getDrawable()); DrawableCompat.setTint(wrap, colorOn); } holder.cbScreenWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.screen_wifi = isChecked; updateRule(rule, true, listAll); } }); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Drawable wrap = DrawableCompat.wrap(holder.ivOtherLegend.getDrawable()); DrawableCompat.setTint(wrap, colorOn); } // Show mobile screen on condition holder.cbScreenOther.setOnCheckedChangeListener(null); holder.cbScreenOther.setChecked(rule.screen_other); holder.cbScreenOther.setEnabled(rule.other_blocked && rule.apply); holder.cbScreenOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.screen_other = isChecked; updateRule(rule, true, listAll); } }); // Show roaming condition holder.cbRoaming.setOnCheckedChangeListener(null); holder.cbRoaming.setChecked(rule.roaming); holder.cbRoaming.setEnabled((!rule.other_blocked || rule.screen_other) && rule.apply); holder.cbRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @TargetApi(Build.VERSION_CODES.M) public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rule.roaming = isChecked; updateRule(rule, true, listAll); // Request permissions if (isChecked && !Util.hasPhoneStatePermission(context)) context.requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE }, ActivityMain.REQUEST_ROAMING); } }); // Reset rule holder.btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Util.areYouSure(view.getContext(), R.string.msg_clear_rules, new Util.DoubtListener() { @Override public void onSure() { holder.cbApply.setChecked(true); holder.cbWifi.setChecked(rule.wifi_default); holder.cbOther.setChecked(rule.other_default); holder.cbScreenWifi.setChecked(rule.screen_wifi_default); holder.cbScreenOther.setChecked(rule.screen_other_default); holder.cbRoaming.setChecked(rule.roaming_default); } }); } }); // Show access rules if (rule.expanded) { // Access the database when expanded only final AdapterAccess badapter = new AdapterAccess(context, DatabaseHelper.getInstance(context).getAccess(rule.info.applicationInfo.uid)); if (filter) holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) { PackageManager pm = context.getPackageManager(); Cursor cursor = (Cursor) badapter.getItem(bposition); final long id = cursor.getLong(cursor.getColumnIndex("ID")); int version = cursor.getInt(cursor.getColumnIndex("version")); int protocol = cursor.getInt(cursor.getColumnIndex("protocol")); String daddr = cursor.getString(cursor.getColumnIndex("daddr")); int dport = cursor.getInt(cursor.getColumnIndex("dport")); long time = cursor.getLong(cursor.getColumnIndex("time")); int block = cursor.getInt(cursor.getColumnIndex("block")); PopupMenu popup = new PopupMenu(context, context.findViewById(R.id.vwPopupAnchor)); popup.inflate(R.menu.access); popup.getMenu().findItem(R.id.menu_host) .setTitle(Util.getProtocolName(protocol, version, false) + " " + daddr + (dport > 0 ? "/" + dport : "")); // Whois final Intent lookupIP = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.tcpiputils.com/whois-lookup/" + daddr)); if (pm.resolveActivity(lookupIP, 0) == null) popup.getMenu().removeItem(R.id.menu_whois); else popup.getMenu().findItem(R.id.menu_whois) .setTitle(context.getString(R.string.title_log_whois, daddr)); // Lookup port final Intent lookupPort = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.speedguide.net/port.php?port=" + dport)); if (dport <= 0 || pm.resolveActivity(lookupPort, 0) == null) popup.getMenu().removeItem(R.id.menu_port); else popup.getMenu().findItem(R.id.menu_port) .setTitle(context.getString(R.string.title_log_port, dport)); popup.getMenu().findItem(R.id.menu_time) .setTitle(SimpleDateFormat.getDateTimeInstance().format(time)); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_whois: context.startActivity(lookupIP); return true; case R.id.menu_port: context.startActivity(lookupPort); return true; case R.id.menu_allow: DatabaseHelper.getInstance(context).setAccess(id, 0); ServiceSinkhole.reload("allow host", context); return true; case R.id.menu_block: DatabaseHelper.getInstance(context).setAccess(id, 1); ServiceSinkhole.reload("block host", context); return true; case R.id.menu_reset: DatabaseHelper.getInstance(context).setAccess(id, -1); ServiceSinkhole.reload("reset host", context); return true; } return false; } }); if (block == 0) popup.getMenu().removeItem(R.id.menu_allow); else if (block == 1) popup.getMenu().removeItem(R.id.menu_block); popup.show(); } }); else holder.lvAccess.setOnItemClickListener(null); holder.lvAccess.setAdapter(badapter); } else { holder.lvAccess.setAdapter(null); holder.lvAccess.setOnItemClickListener(null); } // Show logging is disabled final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean log_app = prefs.getBoolean("log_app", false); holder.tvNolog.setVisibility(log_app ? View.GONE : View.VISIBLE); holder.tvNolog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(new Intent(context, ActivitySettings.class)); } }); // Show disable access notifications setting holder.cbNotify.setOnCheckedChangeListener(null); holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply); holder.cbNotify.setChecked(rule.notify); holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { rule.notify = isChecked; updateRule(rule, true, listAll); } }); // Clear access log holder.btnClearAccess.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Util.areYouSure(view.getContext(), R.string.msg_reset_access, new Util.DoubtListener() { @Override public void onSure() { DatabaseHelper.getInstance(context).clearAccess(rule.info.applicationInfo.uid, true); if (rv != null) rv.scrollToPosition(position); } }); } }); // Show traffic statistics holder.tvStatistics.setText( "?? : " + context.getString(R.string.msg_mbday, rule.upspeed, rule.downspeed)); }