Example usage for android.app Activity getString

List of usage examples for android.app Activity getString

Introduction

In this page you can find the example usage for android.app Activity getString.

Prototype

@NonNull
public final String getString(@StringRes int resId, Object... formatArgs) 

Source Link

Document

Returns a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and java.lang.String#format .

Usage

From source file:com.felkertech.n.ActivityUtils.java

public static void addChannel(final Activity mActivity, GoogleApiClient gapi, CumulusChannel jsonChannel) {
    if (DEBUG) {/*from w  w  w  .  ja  v  a  2  s  .  c  o  m*/
        Log.d(TAG, "I've been told to add " + jsonChannel.toString());
    }
    ChannelDatabase cd = ChannelDatabase.getInstance(mActivity);
    if (cd.channelExists(jsonChannel)) {
        Toast.makeText(mActivity, R.string.channel_dupe, Toast.LENGTH_SHORT).show();
    } else {
        try {
            if (jsonChannel.getName() != null) {
                Toast.makeText(mActivity, mActivity.getString(R.string.channel_added, jsonChannel.getName()),
                        Toast.LENGTH_SHORT).show();
            }
            cd.add(jsonChannel);
            ActivityUtils.writeDriveData(mActivity, gapi);
            if (DEBUG) {
                Log.d(TAG, "Added");
            }
            /*new Thread(new Runnable() {
            @Override
            public void run() {
                // Resync
                SyncUtils.requestSync(mActivity,
                        ActivityUtils.TV_INPUT_SERVICE.flattenToString());
            }
            }).start();*/
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.normsstuff.maps4norm.Dialogs.java

public static void loadMarks(final Activity c, final Stack<LatLng> trace) {
    File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
            && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File ext = API8Wrapper.getExternalFilesDir(c);
        // even though we checked the external storage state, ext is still sometimes null,
        // according to Play Store crash reports
        if (ext != null) {
            File[] filesExtern = ext.listFiles();
            File[] allFiles = new File[files.length + filesExtern.length];
            System.arraycopy(files, 0, allFiles, 0, files.length);
            System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
            files = allFiles;/*from  w w  w.  java 2 s  .c om*/
        }
    }

    if (files.length == 0) {
        Toast.makeText(c, c.getString(R.string.no_files_found,
                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()), Toast.LENGTH_LONG).show();

    } else if (files.length == 1) {
        try {
            List<LatLng> list = Util.loadFromFile(Uri.fromFile(files[0]), (MyMapActivity) c);
            setUpDisplay((MyMapActivity) c, list);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(c, c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                    Toast.LENGTH_LONG).show();
        }
    } else {
        AlertDialog.Builder b = new AlertDialog.Builder(c);
        b.setTitle(R.string.select_file);
        final DeleteAdapter da = new DeleteAdapter(files, (MyMapActivity) c);
        b.setAdapter(da, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    List<LatLng> list = Util.loadFromFile(Uri.fromFile(da.getFile(which)), (MyMapActivity) c);
                    dialog.dismiss();
                    setUpDisplay((MyMapActivity) c, list);
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        b.create().show();
    }
}

From source file:de.j4velin.mapsmeasure.Dialogs.java

/**
 * @param c     the Context//ww  w. j a  v a2 s  . c  o m
 * @param trace the current trace of points
 * @return the "save & share" dialog
 */
public static Dialog getSaveNShare(final Activity c, final Stack<LatLng> trace) {
    final Dialog d = new Dialog(c);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.dialog_save);
    d.findViewById(R.id.save).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            final File destination;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                destination = API8Wrapper.getExternalFilesDir(c);
            } else {
                destination = c.getDir("traces", Context.MODE_PRIVATE);
            }

            d.dismiss();
            AlertDialog.Builder b = new AlertDialog.Builder(c);
            b.setTitle(R.string.save);
            final View layout = c.getLayoutInflater().inflate(R.layout.dialog_enter_filename, null);
            ((TextView) layout.findViewById(R.id.location))
                    .setText(c.getString(R.string.file_path, destination.getAbsolutePath() + "/"));
            b.setView(layout);
            b.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        String fname = ((EditText) layout.findViewById(R.id.filename)).getText().toString();
                        if (fname == null || fname.length() < 1) {
                            fname = "MapsMeasure_" + System.currentTimeMillis();
                        }
                        final File f = new File(destination, fname + ".csv");
                        Util.saveToFile(f, trace);
                        d.dismiss();
                        Toast.makeText(c, c.getString(R.string.file_saved, f.getAbsolutePath()),
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(c,
                                c.getString(R.string.error,
                                        e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            b.create().show();
        }
    });
    d.findViewById(R.id.load).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {

            File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File ext = API8Wrapper.getExternalFilesDir(c);
                // even though we checked the external storage state, ext is still sometims null, accoring to Play Store crash reports
                if (ext != null) {
                    File[] filesExtern = ext.listFiles();
                    File[] allFiles = new File[files.length + filesExtern.length];
                    System.arraycopy(files, 0, allFiles, 0, files.length);
                    System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
                    files = allFiles;
                }
            }

            if (files.length == 0) {
                Toast.makeText(c,
                        c.getString(R.string.no_files_found,
                                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()),
                        Toast.LENGTH_SHORT).show();
            } else if (files.length == 1) {
                try {
                    Util.loadFromFile(Uri.fromFile(files[0]), (Map) c);
                    d.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            } else {
                d.dismiss();
                AlertDialog.Builder b = new AlertDialog.Builder(c);
                b.setTitle(R.string.select_file);
                final DeleteAdapter da = new DeleteAdapter(files, (Map) c);
                b.setAdapter(da, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Util.loadFromFile(Uri.fromFile(da.getFile(which)), (Map) c);
                            dialog.dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(c,
                                    c.getString(R.string.error,
                                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                });
                b.create().show();
            }
        }
    });
    d.findViewById(R.id.share).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            try {
                final File f = new File(c.getCacheDir(), "MapsMeasure.csv");
                Util.saveToFile(f, trace);
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM,
                        FileProvider.getUriForFile(c, "de.j4velin.mapsmeasure.fileprovider", f));
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                shareIntent.setType("text/comma-separated-values");
                d.dismiss();
                c.startActivity(Intent.createChooser(shareIntent, null));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(c,
                        c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
    return d;
}

From source file:com.normsstuff.maps4norm.Dialogs.java

/**
 * @param c     the Context/*from www . j a  v  a2s. co m*/
 * @param trace the current trace of points
 * @return the "save & share" dialog
 */
public static Dialog getSaveNShare(final Activity c, final Stack<LatLng> trace) {
    final Dialog d = new Dialog(c);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.dialog_save);
    d.findViewById(R.id.saveMarks).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View vX) {
            final File destination;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                destination = API8Wrapper.getExternalFilesDir(c);
            } else {
                destination = c.getDir("traces", Context.MODE_PRIVATE);
            }

            d.dismiss();
            AlertDialog.Builder b = new AlertDialog.Builder(c);
            b.setTitle(R.string.save);
            final View layout = c.getLayoutInflater().inflate(R.layout.dialog_enter_filename, null);
            ((TextView) layout.findViewById(R.id.location))
                    .setText(c.getString(R.string.file_path, destination.getAbsolutePath() + "/"));
            b.setView(layout);
            b.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        String fname = ((EditText) layout.findViewById(R.id.filename)).getText().toString();
                        if (fname == null || fname.length() < 1) {
                            fname = "MapsMeasure_" + System.currentTimeMillis();
                        }
                        final File f = new File(destination, fname + ".csv");
                        Util.saveToFile(f, trace);
                        d.dismiss();
                        Toast.makeText(c, c.getString(R.string.file_saved, f.getAbsolutePath()),
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(c,
                                c.getString(R.string.error,
                                        e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            b.create().show();
        }
    });
    d.findViewById(R.id.loadMarks).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {

            File[] files = c.getDir("traces", Context.MODE_PRIVATE).listFiles();

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO
                    && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File ext = API8Wrapper.getExternalFilesDir(c);
                // even though we checked the external storage state, ext is still sometimes null, accoring to Play Store crash reports
                if (ext != null) {
                    File[] filesExtern = ext.listFiles();
                    File[] allFiles = new File[files.length + filesExtern.length];
                    System.arraycopy(files, 0, allFiles, 0, files.length);
                    System.arraycopy(filesExtern, 0, allFiles, files.length, filesExtern.length);
                    files = allFiles;
                }
            }

            if (files.length == 0) {
                Toast.makeText(c,
                        c.getString(R.string.no_files_found,
                                c.getDir("traces", Context.MODE_PRIVATE).getAbsolutePath()),
                        Toast.LENGTH_SHORT).show();
            } else if (files.length == 1) {
                try {
                    Util.loadFromFile(Uri.fromFile(files[0]), (MyMapActivity) c);
                    d.dismiss();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(c,
                            c.getString(R.string.error, e.getClass().getSimpleName() + "\n" + e.getMessage()),
                            Toast.LENGTH_LONG).show();
                }
            } else {
                d.dismiss();
                AlertDialog.Builder b = new AlertDialog.Builder(c);
                b.setTitle(R.string.select_file);
                final DeleteAdapter da = new DeleteAdapter(files, (MyMapActivity) c);
                b.setAdapter(da, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Util.loadFromFile(Uri.fromFile(da.getFile(which)), (MyMapActivity) c);
                            dialog.dismiss();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Toast.makeText(c,
                                    c.getString(R.string.error,
                                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                });
                b.create().show();
            }
        }
    });
    /*        
            d.findViewById(R.id.share).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(final View v) {
        try {
            final File f = new File(c.getCacheDir(), "MapsMeasure.csv");
            Util.saveToFile(f, trace);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, FileProvider
                    .getUriForFile(c, "de.j4velin.mapsmeasure.fileprovider", f));
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.setType("text/comma-separated-values");
            d.dismiss();
            c.startActivity(Intent.createChooser(shareIntent, null));
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(c, c.getString(R.string.error,
                            e.getClass().getSimpleName() + "\n" + e.getMessage()),
                    Toast.LENGTH_LONG).show();
        }
    }
            });
    */
    return d;
}

From source file:org.sufficientlysecure.keychain.ui.linked.LinkedIdCreateGithubFragment.java

private void step2PostGist(final String accessToken, final String gistText) {

    setState(State.POST_PROCESS);

    new AsyncTask<Void, Void, JSONObject>() {

        Exception mException;/*from  www. ja  v a2 s. co  m*/

        @Override
        protected JSONObject doInBackground(Void... dummy) {
            try {

                long timer = System.currentTimeMillis();

                JSONObject file = new JSONObject();
                file.put("content", gistText);

                JSONObject files = new JSONObject();
                files.put("openpgp.txt", file);

                JSONObject params = new JSONObject();
                params.put("public", true);
                params.put("description", getString(R.string.linked_gist_description));
                params.put("files", files);

                JSONObject result = jsonHttpRequest("https://api.github.com/gists", params, accessToken);

                // ux flow: this operation should take at last a second
                timer = System.currentTimeMillis() - timer;
                if (timer < 1000)
                    try {
                        Thread.sleep(1000 - timer);
                    } catch (InterruptedException e) {
                        // never mind
                    }

                return result;

            } catch (IOException | HttpResultException e) {
                mException = e;
            } catch (JSONException e) {
                throw new AssertionError("json error, this is a bug!");
            }
            return null;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);

            Log.d(Constants.TAG, "response: " + result);

            Activity activity = getActivity();
            if (activity == null) {
                // we couldn't show an error anyways
                return;
            }

            if (result == null) {
                setState(State.POST_ERROR);
                showRetryForOAuth();

                if (mException instanceof SocketTimeoutException) {
                    Notify.create(activity, R.string.linked_error_timeout, Style.ERROR).show();
                } else if (mException instanceof HttpResultException) {
                    Notify.create(activity, activity.getString(R.string.linked_error_http,
                            ((HttpResultException) mException).mResponse), Style.ERROR).show();
                } else if (mException instanceof IOException) {
                    Notify.create(activity, R.string.linked_error_network, Style.ERROR).show();
                }

                return;
            }

            GithubResource resource;

            try {
                String gistId = result.getString("id");
                JSONObject owner = result.getJSONObject("owner");
                String gistLogin = owner.getString("login");

                URI uri = URI.create("https://gist.github.com/" + gistLogin + "/" + gistId);
                resource = GithubResource.create(uri);
            } catch (JSONException e) {
                setState(State.POST_ERROR);
                return;
            }

            View linkedItem = mButtonContainer.getChildAt(2);
            if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
                linkedItem.setTransitionName(resource.toUri().toString());
            }

            // we only need authorization for this one operation, drop it afterwards
            revokeToken(accessToken);

            step3EditKey(resource);
        }

    }.execute();

}

From source file:org.sufficientlysecure.keychain.ui.linked.LinkedIdCreateGithubFragment.java

private void step1GetOAuthToken() {

    if (mOAuthCode == null) {
        setState(State.AUTH_ERROR);
        showRetryForOAuth();/*from  ww w .j  av  a2s .com*/
        return;
    }

    Activity activity = getActivity();
    if (activity == null) {
        return;
    }

    final String gistText = GithubResource.generate(activity, mFingerprint);

    new AsyncTask<Void, Void, JSONObject>() {

        Exception mException;

        @Override
        protected JSONObject doInBackground(Void... dummy) {
            try {

                JSONObject params = new JSONObject();
                params.put("client_id", BuildConfig.GITHUB_CLIENT_ID);
                params.put("client_secret", BuildConfig.GITHUB_CLIENT_SECRET);
                params.put("code", mOAuthCode);
                params.put("state", mOAuthState);

                return jsonHttpRequest("https://github.com/login/oauth/access_token", params, null);

            } catch (IOException | HttpResultException e) {
                mException = e;
            } catch (JSONException e) {
                throw new AssertionError("json error, this is a bug!");
            }
            return null;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);

            Activity activity = getActivity();
            if (activity == null) {
                // we couldn't show an error anyways
                return;
            }

            Log.d(Constants.TAG, "response: " + result);

            if (result == null || result.optString("access_token", null) == null) {
                setState(State.AUTH_ERROR);
                showRetryForOAuth();

                if (result != null) {
                    Notify.create(activity, R.string.linked_error_auth_failed, Style.ERROR).show();
                    return;
                }

                if (mException instanceof SocketTimeoutException) {
                    Notify.create(activity, R.string.linked_error_timeout, Style.ERROR).show();
                } else if (mException instanceof HttpResultException) {
                    Notify.create(activity, activity.getString(R.string.linked_error_http,
                            ((HttpResultException) mException).mResponse), Style.ERROR).show();
                } else if (mException instanceof IOException) {
                    Notify.create(activity, R.string.linked_error_network, Style.ERROR).show();
                }

                return;
            }

            step2PostGist(result.optString("access_token"), gistText);

        }
    }.execute();

}

From source file:com.hybris.mobile.app.commerce.helper.OrderHelper.java

/**
 * Populate the order summary//w  w  w.java 2s.c  o  m
 *
 * @param order
 */
public static void createOrderSummary(Activity activity, Order order) {

    LinearLayout mOrderSummaryItemsLayout;
    TextView mOrderSummaryItems;
    TextView mOrderSummarySubtotal;
    TextView mOrderSummarySavings;
    TextView mOrderSummaryTax;
    TextView mOrderSummaryShipping;
    TextView mOrderSummaryTotal;
    TextView mOrderSummaryPromotion;
    TableRow mOrderSummarySavingsRow;

    // order summary
    mOrderSummaryItemsLayout = (LinearLayout) activity.findViewById(R.id.order_summary_items_layout);
    mOrderSummaryItems = (TextView) activity.findViewById(R.id.order_summary_items);
    mOrderSummarySubtotal = (TextView) activity.findViewById(R.id.order_summary_subtotal);
    mOrderSummarySavings = (TextView) activity.findViewById(R.id.order_summary_savings);
    mOrderSummaryTax = (TextView) activity.findViewById(R.id.order_summary_tax);
    mOrderSummaryShipping = (TextView) activity.findViewById(R.id.order_summary_shipping);
    mOrderSummaryTotal = (TextView) activity.findViewById(R.id.order_summary_total);
    mOrderSummaryPromotion = (TextView) activity.findViewById(R.id.order_summary_promotion);
    mOrderSummarySavingsRow = (TableRow) activity.findViewById(R.id.order_summary_savings_row);

    if (order != null) {

        populatePromotions(order);

        // Display total price
        if (order.getTotalPrice() != null) {
            mOrderSummaryTotal.setText(order.getTotalPrice().getFormattedValue());
        }

        // Display subtotal price
        if (order.getSubTotal() != null) {
            mOrderSummarySubtotal.setText(order.getSubTotal().getFormattedValue());
        }

        // Display tax price
        if (order.getTotalTax() != null) {
            mOrderSummaryTax.setText(order.getTotalTax().getFormattedValue());
        }

        // Display delivery method cost
        if (order.getDeliveryCost() != null) {
            mOrderSummaryShipping.setText(order.getDeliveryCost().getFormattedValue());
        }

        if (order.getAppliedOrderPromotions() != null && !order.getAppliedOrderPromotions().isEmpty()) {
            if (StringUtils.isNotBlank(order.getOrderDiscounts().getFormattedValue())) {
                mOrderSummarySavingsRow.setVisibility(View.VISIBLE);
                mOrderSummarySavings.setText(order.getOrderDiscounts().getFormattedValue());
            }
        }

        if (order.getAppliedOrderPromotions() != null || order.getAppliedProductPromotions() != null) {
            if (order.getAppliedProductPromotions() != null && !order.getAppliedProductPromotions().isEmpty()) {
                mOrderSummaryPromotion.setVisibility(View.VISIBLE);
                // Nb order Promotion
                StringBuffer promotion = new StringBuffer();

                if (order.getAppliedOrderPromotions() != null && !order.getAppliedOrderPromotions().isEmpty()) {
                    for (PromotionResult orderPromotion : order.getAppliedOrderPromotions()) {
                        promotion.append(orderPromotion.getDescription()).append("\n");
                    }
                }

                mOrderSummaryPromotion.setText(promotion);
            } else {
                mOrderSummaryPromotion.setVisibility(View.GONE);
            }
        } else {
            mOrderSummaryPromotion.setVisibility(View.GONE);
            mOrderSummarySavingsRow.setVisibility(View.GONE);
        }

        // Nb items
        mOrderSummaryItemsLayout.setVisibility(View.VISIBLE);
        mOrderSummaryItems
                .setText(activity.getString(R.string.order_summary_items, order.getDeliveryItemsQuantity()));
    }
}