Example usage for android.content ComponentName unflattenFromString

List of usage examples for android.content ComponentName unflattenFromString

Introduction

In this page you can find the example usage for android.content ComponentName unflattenFromString.

Prototype

public static @Nullable ComponentName unflattenFromString(@NonNull String str) 

Source Link

Document

Recover a ComponentName from a String that was previously created with #flattenToString() .

Usage

From source file:ca.ualberta.cs.drivr.RequestsListAdapter.java

/**
 * Called when the view holder is wants to bind the request at a certain position in the list.
 * @param viewHolder/* w  w  w  .  j a  v a 2s. com*/
 * @param position
 */
@Override
public void onBindViewHolder(final RequestsListAdapter.ViewHolder viewHolder, final int position) {
    final Request request = requestsToDisplay.get(position);

    // Get the views to update
    final TextView otherUserNameTextView = viewHolder.otherUserNameTextView;
    final TextView descriptionTextView = viewHolder.descriptionTextView;
    final TextView fareTextView = viewHolder.fareTextView;
    final TextView routeTextView = viewHolder.routeTextView;
    final TextView statusTextView = viewHolder.statusTextView;
    final ImageView callImageView = viewHolder.callImageView;
    final ImageView emailImageView = viewHolder.emailImageView;
    final ImageView checkImageView = viewHolder.checkMarkImageView;
    final ImageView deleteImageView = viewHolder.xMarkImageView;

    // Todo Hide Image Views until correct Request State
    if (request.getRequestState() != RequestState.CONFIRMED) {
        checkImageView.setVisibility(View.INVISIBLE);
    }

    if (request.getRequestState() != RequestState.PENDING) {
        deleteImageView.setVisibility(View.INVISIBLE);
    }

    // Show the other person's name
    final DriversList drivers = request.getDrivers();

    // Get the username of the other user
    if (userManager.getUserMode() == UserMode.RIDER) {
        final String multipleDrivers = "Multiple Drivers Accepted";
        final String driverUsername = drivers.size() == 1 ? drivers.get(0).getUsername() : "No Driver Yet";
        otherUserNameTextView.setText(drivers.size() > 1 ? multipleDrivers : driverUsername);
    } else {
        otherUserNameTextView.setText(request.getRider().getUsername());
    }

    // If the request has a description, show it. Otherwise, hide te description
    if (Strings.isNullOrEmpty(request.getDescription()))
        descriptionTextView.setVisibility(View.GONE);
    else
        descriptionTextView.setText(request.getDescription());

    // Show the fare
    fareTextView.setText("$" + request.getFareString());

    // Show the route
    routeTextView.setText(request.getRoute());

    // Driver User
    otherUserNameTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String otherUsername = otherUserNameTextView.getText().toString();
            // there exists drivers
            if (otherUsername != "No Driver Yet") {
                if (otherUsername != "Multiple Drivers Accepted") {
                    Gson gson = new GsonBuilder().registerTypeAdapter(Uri.class, new UriSerializer()).create();

                    ElasticSearch elasticSearch = new ElasticSearch(
                            UserManager.getInstance().getConnectivityManager());
                    User user = elasticSearch.loadUser(otherUsername);

                    String driverString = gson.toJson(user, User.class);
                    Intent intent = new Intent(context, DriverProfileActivity.class);
                    intent.putExtra(DriverProfileActivity.DRIVER, driverString);
                    context.startActivity(intent);
                } else {
                    startMultipleDriverIntent(request);
                }
            }
        }
    });

    routeTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Gson gson = new GsonBuilder().registerTypeAdapter(Uri.class, new UriSerializer()).create();
            String requestString = gson.toJson(request, Request.class);
            Intent intent = new Intent(context, RequestActivity.class);
            intent.putExtra("UniqueID", "From_RequestListActivity");
            intent.putExtra(RequestActivity.EXTRA_REQUEST, requestString);
            context.startActivity(intent);
        }
    });

    // Show the status text
    statusTextView.setText(request.getRequestState().toString());

    // Add a listener to the call image
    callImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (drivers.size() == 0) {
                Toast.makeText(context, "No driver number available at this time", Toast.LENGTH_SHORT).show();

            }
            // Start Dialer
            else if (drivers.size() == 1) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                String number;
                if (UserManager.getInstance().getUserMode().equals(UserMode.RIDER)) {
                    number = drivers.get(0).getPhoneNumber();
                } else {
                    number = request.getRider().getPhoneNumber();
                }
                number = "tel:" + number;
                intent.setData(Uri.parse(number));
                if (ActivityCompat.checkSelfPermission(context,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                context.startActivity(intent);

            } else {

                startMultipleDriverIntent(request);
            }
        }
    });

    // Add a listener to the email image
    emailImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (drivers.size() == 0) {
                Toast.makeText(context, "No driver email available at this time", Toast.LENGTH_SHORT).show();

            }
            //http://stackoverflow.com/questions/8701634/send-email-intent
            else if (drivers.size() == 1) {

                Intent intent = new Intent();
                ComponentName emailApp = intent.resolveActivity(context.getPackageManager());
                ComponentName unsupportedAction = ComponentName
                        .unflattenFromString("com.android.fallback/.Fallback");
                boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);
                String email;

                if (UserManager.getInstance().getUserMode().equals(UserMode.RIDER)) {
                    email = drivers.get(0).getEmail();
                } else {
                    email = request.getRider().getEmail();
                }
                String subject = "Drivr Request: " + request.getId();
                String body = "Drivr user " + drivers.get(0).getUsername();

                if (hasEmailApp) {
                    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    emailIntent.putExtra(Intent.EXTRA_TEXT, body);
                    context.startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
                } else {
                    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", email, null));
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "");
                    emailIntent.putExtra(Intent.EXTRA_TEXT, "");
                    context.startActivity(Intent.createChooser(emailIntent, "Send email..."));
                }
            } else {
                startMultipleDriverIntent(request);

            }
        }
    });

    // Complete The Request
    checkImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, RequestCompletedActivity.class);
            intent.putExtra(RequestCompletedActivity.REQUEST_ID_EXTRA, request.getId());
            context.startActivity(intent);
        }
    });

    deleteImageView.setOnClickListener(new View.OnClickListener() {

        // Todo Delete the Request
        @Override
        public void onClick(View v) {
            v.getContext();
            ElasticSearch elasticSearch = new ElasticSearch(
                    (ConnectivityManager) v.getContext().getSystemService(Context.CONNECTIVITY_SERVICE));
            elasticSearch.deleteRequest(request.getId());
            UserManager userManager = UserManager.getInstance();
            userManager.getRequestsList().removeById(request);
            userManager.notifyObservers();
            requestsToDisplay.remove(request);
            notifyItemRemoved(viewHolder.getAdapterPosition());
        }
    });
}

From source file:com.google.android.apps.dashclock.ExtensionManager.java

/**
 * Returns a listing of all available (installed) extensions, including those that aren't
 * world-readable./*from  w  w  w  .  ja  v  a  2  s  .co m*/
 */
public List<ExtensionListing> getAvailableExtensions() {
    List<ExtensionListing> availableExtensions = new ArrayList<>();
    PackageManager pm = mApplicationContext.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(DashClockExtension.ACTION_EXTENSION),
            PackageManager.GET_META_DATA);
    for (ResolveInfo resolveInfo : resolveInfos) {
        ExtensionListing info = new ExtensionListing();
        info.componentName(
                new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name));
        info.title(resolveInfo.loadLabel(pm).toString());
        Bundle metaData = resolveInfo.serviceInfo.metaData;
        if (metaData != null) {
            info.compatible(ExtensionHost.supportsProtocolVersion(metaData.getInt("protocolVersion")));
            info.worldReadable(metaData.getBoolean("worldReadable", false));
            info.description(metaData.getString("description"));
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                info.settingsActivity(ComponentName
                        .unflattenFromString(resolveInfo.serviceInfo.packageName + "/" + settingsActivity));
            }
        }

        info.icon(resolveInfo.getIconResource());
        availableExtensions.add(info);
    }

    return availableExtensions;
}

From source file:com.androidzeitgeist.dashwatch.dashclock.ExtensionManager.java

/**
 * Returns a listing of all available (installed) extensions.
 *///ww w .  j  a  va 2s .c o m
public List<ExtensionListing> getAvailableExtensions() {
    List<ExtensionListing> availableExtensions = new ArrayList<ExtensionListing>();
    PackageManager pm = mApplicationContext.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(DashClockExtension.ACTION_EXTENSION),
            PackageManager.GET_META_DATA);
    for (ResolveInfo resolveInfo : resolveInfos) {
        ExtensionListing listing = new ExtensionListing();
        listing.componentName = new ComponentName(resolveInfo.serviceInfo.packageName,
                resolveInfo.serviceInfo.name);
        listing.title = resolveInfo.loadLabel(pm).toString();
        Bundle metaData = resolveInfo.serviceInfo.metaData;
        if (metaData != null) {
            listing.protocolVersion = metaData.getInt("protocolVersion");
            listing.worldReadable = metaData.getBoolean("worldReadable", false);
            listing.description = metaData.getString("description");
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                listing.settingsActivity = ComponentName
                        .unflattenFromString(resolveInfo.serviceInfo.packageName + "/" + settingsActivity);
            }
        }

        listing.icon = resolveInfo.loadIcon(pm);
        availableExtensions.add(listing);
    }

    return availableExtensions;
}

From source file:uk.co.tekkies.readings.fragment.PassageFragment.java

private void openMySwordPassage() {
    Analytics.UIClick(getActivity(), "open-mysword-passage");
    try {/* w  w w . j ava  2  s.  c o m*/
        Intent intent = new Intent();
        intent.setComponent(ComponentName.unflattenFromString(
                "com.riversoft.android.mysword/com.riversoft.android.mysword.MySwordLink"));
        intent.setData(Uri.parse("http://mysword.info/b?r=" + passage));
        startActivity(intent);
    } catch (Exception e) {
        Analytics.reportCaughtException(getActivity(), e);
    }
}

From source file:com.google.android.apps.dashclock.ExtensionManager.java

private void loadInternalActiveExtensionList() {
    List<ComponentName> activeExtensions = new ArrayList<>();
    String extensions;/*from  w  w  w .jav  a2  s.  com*/
    if (mDefaultPreferences.contains(PREF_ACTIVE_EXTENSIONS)) {
        extensions = mDefaultPreferences.getString(PREF_ACTIVE_EXTENSIONS, "");
    } else {
        extensions = createDefaultExtensionList();
    }
    String[] componentNameStrings = extensions.split(",");
    for (String componentNameString : componentNameStrings) {
        if (TextUtils.isEmpty(componentNameString)) {
            continue;
        }
        activeExtensions.add(ComponentName.unflattenFromString(componentNameString));
    }
    setInternalActiveExtensions(activeExtensions);
}

From source file:com.actionlauncher.api.LiveWallpaperSource.java

private synchronized void loadSubscriptions() {
    mSubscriptions = new HashMap<ComponentName, String>();
    Set<String> serializedSubscriptions = mSharedPrefs.getStringSet(PREF_SUBSCRIPTIONS, null);
    if (serializedSubscriptions != null) {
        for (String serializedSubscription : serializedSubscriptions) {
            String[] arr = serializedSubscription.split("\\|", 2);
            ComponentName subscriber = ComponentName.unflattenFromString(arr[0]);
            String token = arr[1];
            mSubscriptions.put(subscriber, token);
        }//from  ww w . j a  v  a  2  s.co  m
    }
}

From source file:com.battlelancer.seriesguide.extensions.ExtensionManager.java

private synchronized void loadSubscriptions() {
    mEnabledExtensions = new ArrayList<>();
    mSubscriptions = new HashMap<>();
    mTokens = new HashMap<>();

    String serializedSubscriptions = mSharedPrefs.getString(PREF_SUBSCRIPTIONS, null);
    if (serializedSubscriptions == null) {
        setDefaultEnabledExtensions();//w w  w . j  av  a  2  s. com
        return;
    }

    JSONArray jsonArray;
    try {
        jsonArray = new JSONArray(serializedSubscriptions);
    } catch (JSONException e) {
        Timber.e(e, "Deserializing subscriptions failed");
        return;
    }

    for (int i = 0; i < jsonArray.length(); i++) {
        String subscription = jsonArray.optString(i, null);
        if (subscription == null) {
            continue;
        }
        String[] arr = subscription.split("\\|", 2);
        ComponentName extension = ComponentName.unflattenFromString(arr[0]);
        String token = arr[1];
        mEnabledExtensions.add(extension);
        mSubscriptions.put(extension, token);
        mTokens.put(token, extension);
        Timber.d("Restored subscription: " + extension + " token: " + token);
    }
}

From source file:com.battlelancer.seriesguide.api.SeriesGuideExtension.java

private synchronized void loadSubscriptions() {
    mSubscribers = new HashMap<ComponentName, String>();
    Set<String> serializedSubscriptions = mSharedPrefs.getStringSet(PREF_SUBSCRIPTIONS, null);
    if (serializedSubscriptions != null) {
        for (String serializedSubscription : serializedSubscriptions) {
            String[] arr = serializedSubscription.split("\\|", 2);
            ComponentName subscriber = ComponentName.unflattenFromString(arr[0]);
            String token = arr[1];
            mSubscribers.put(subscriber, token);
        }/* ww  w.  ja va2  s .co  m*/
    }
}

From source file:com.google.android.apps.muzei.settings.SettingsChooseSourceFragment.java

public void updateSources() {
    mSelectedSource = null;/*from   w  w w  . j a  v  a  2 s  .  c o m*/
    Intent queryIntent = new Intent(ACTION_MUZEI_ART_SOURCE);
    PackageManager pm = getActivity().getPackageManager();
    mSources.clear();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, PackageManager.GET_META_DATA);

    for (ResolveInfo ri : resolveInfos) {
        Source source = new Source();
        source.label = ri.loadLabel(pm).toString();
        source.icon = new BitmapDrawable(getResources(), generateSourceImage(ri.loadIcon(pm)));
        source.componentName = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        if (ri.serviceInfo.descriptionRes != 0) {
            try {
                Context packageContext = getActivity()
                        .createPackageContext(source.componentName.getPackageName(), 0);
                Resources packageRes = packageContext.getResources();
                source.description = packageRes.getString(ri.serviceInfo.descriptionRes);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Can't read package resources for source " + source.componentName);
            }
        }
        Bundle metaData = ri.serviceInfo.metaData;
        source.color = Color.WHITE;
        if (metaData != null) {
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                source.settingsActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + settingsActivity);
            }

            String setupActivity = metaData.getString("setupActivity");
            if (!TextUtils.isEmpty(setupActivity)) {
                source.setupActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + setupActivity);
            }

            source.color = metaData.getInt("color", source.color);

            try {
                float[] hsv = new float[3];
                Color.colorToHSV(source.color, hsv);
                boolean adjust = false;
                if (hsv[2] < 0.8f) {
                    hsv[2] = 0.8f;
                    adjust = true;
                }
                if (hsv[1] > 0.4f) {
                    hsv[1] = 0.4f;
                    adjust = true;
                }
                if (adjust) {
                    source.color = Color.HSVToColor(hsv);
                }
                if (Color.alpha(source.color) != 255) {
                    source.color = Color.argb(255, Color.red(source.color), Color.green(source.color),
                            Color.blue(source.color));
                }
            } catch (IllegalArgumentException ignored) {
            }
        }

        mSources.add(source);
    }

    final String appPackage = getActivity().getPackageName();
    Collections.sort(mSources, new Comparator<Source>() {
        @Override
        public int compare(Source s1, Source s2) {
            String pn1 = s1.componentName.getPackageName();
            String pn2 = s2.componentName.getPackageName();
            if (!pn1.equals(pn2)) {
                if (appPackage.equals(pn1)) {
                    return -1;
                } else if (appPackage.equals(pn2)) {
                    return 1;
                }
            }
            return s1.label.compareTo(s2.label);
        }
    });

    redrawSources();
}

From source file:fr.bmartel.android.fadecandy.service.FadecandyService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //this is the activity which will be opened when user click on notification
    Intent testIntent = new Intent();
    if (intent != null && intent.hasExtra(Constants.SERVICE_EXTRA_ACTIVITY)) {
        testIntent.setComponent(// ww w .  j  a  v  a2s  . c om
                ComponentName.unflattenFromString(intent.getStringExtra(Constants.SERVICE_EXTRA_ACTIVITY)));
    }

    ServiceType serviceTypeOverride;

    if (intent != null && intent.hasExtra(Constants.SERVICE_EXTRA_SERVICE_TYPE)) {
        serviceTypeOverride = ServiceType.getServiceType(intent.getIntExtra(
                Constants.SERVICE_EXTRA_SERVICE_TYPE, ServiceType.NON_PERSISTENT_SERVICE.ordinal()));
    } else {
        serviceTypeOverride = mServiceType;
    }

    switch (serviceTypeOverride) {
    case NON_PERSISTENT_SERVICE:
        Log.v(TAG, "starting NON_PERSISTENT_SERVICE...");
        return START_NOT_STICKY;
    case PERSISTENT_SERVICE:
        startForeground(4242, NotificationHelper.createNotification(this, null, testIntent));
        Log.v(TAG, "starting PERSISTENT_SERVICE...");
        return START_STICKY;
    }

    return START_NOT_STICKY;
}