Search in sources :

Example 56 with Callback

use of zipkin2.Callback in project mapbox-events-android by mapbox.

the class TelemetryClientTest method receivesNoBodyPostingAnEventSuccessfully.

@Test
public void receivesNoBodyPostingAnEventSuccessfully() throws Exception {
    Context mockedContext = mock(Context.class, RETURNS_DEEP_STUBS);
    MapboxTelemetry.applicationContext = mockedContext;
    TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "anyUserAgent");
    List<Event> theEvent = obtainAnEvent();
    Callback mockedCallback = mock(Callback.class);
    enqueueMockNoResponse(204);
    telemetryClient.sendEvents(theEvent, mockedCallback);
    assertResponseBodyEquals(null);
}
Also used : Context(android.content.Context) Callback(okhttp3.Callback) Test(org.junit.Test)

Example 57 with Callback

use of zipkin2.Callback in project mapbox-events-android by mapbox.

the class TelemetryClientTest method parsesInvalidMessageBodyResponseProperlyPostingAnEvent.

@Test
public void parsesInvalidMessageBodyResponseProperlyPostingAnEvent() throws Exception {
    Context mockedContext = mock(Context.class, RETURNS_DEEP_STUBS);
    MapboxTelemetry.applicationContext = mockedContext;
    TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "anyUserAgent");
    List<Event> theEvent = obtainAnEvent();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> bodyRef = new AtomicReference<>();
    final AtomicBoolean failureRef = new AtomicBoolean();
    Callback aCallback = provideACallback(latch, bodyRef, failureRef);
    enqueueMockResponse(422, "invalidMessageBodyResponse.json");
    telemetryClient.sendEvents(theEvent, aCallback);
    latch.await();
    assertTelemetryResponseEquals(bodyRef.get(), "Invalid message body, check the types and required properties of " + "the events you sent");
    assertFalse(failureRef.get());
}
Also used : Context(android.content.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(okhttp3.Callback) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with Callback

use of zipkin2.Callback in project mapbox-events-android by mapbox.

the class TelemetryClientTest method sendsUserAgentHeader.

@Test
public void sendsUserAgentHeader() throws Exception {
    Context mockedContext = mock(Context.class, RETURNS_DEEP_STUBS);
    MapboxTelemetry.applicationContext = mockedContext;
    TelemetryClient telemetryClient = obtainATelemetryClient("anyAccessToken", "theUserAgent");
    List<Event> mockedEvent = obtainAnEvent();
    Callback mockedCallback = mock(Callback.class);
    enqueueMockResponse();
    telemetryClient.sendEvents(mockedEvent, mockedCallback);
    assertRequestContainsHeader("User-Agent", "theUserAgent");
}
Also used : Context(android.content.Context) Callback(okhttp3.Callback) Test(org.junit.Test)

Example 59 with Callback

use of zipkin2.Callback in project Alphago by Onedelay.

the class SendImageActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_image);
    imageFile = (File) getIntent().getSerializableExtra("sendImage");
    cropImageView = findViewById(R.id.cropImageView);
    cropImageView.setImageUriAsync(Uri.fromFile(imageFile));
    cropImageView.setAspectRatio(1, 1);
    cropImageView.setFixedAspectRatio(true);
    cropImageView.setCropShape(CropImageView.CropShape.RECTANGLE);
    cropImageView.setAutoZoomEnabled(false);
    cropImageView.setCropRect(new Rect(1000, 1000, 1000, 1000));
    cropImageView.setOnCropImageCompleteListener(new CropImageView.OnCropImageCompleteListener() {

        @Override
        public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
            Bitmap bitmap = result.getBitmap();
            if (bitmap != null) {
                try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                    byte[] bitmapData = bos.toByteArray();
                    FileOutputStream fos = new FileOutputStream(imageFile);
                    fos.write(bitmapData);
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // onComplete(imageFile);
            // new BitmapToFileTask(imageFile, SendImageActivity.this)
            // .execute(bitmap);
            AlphagoServer.getInstance().sendImage(getBaseContext(), imageFile, new Callback<ResponeImageLabel>() {

                @Override
                public void onResponse(Call<ResponeImageLabel> call, Response<ResponeImageLabel> response) {
                    if (response.body() != null) {
                        category = response.body().getCategory();
                        max_label = response.body().getResponseLabel();
                        ID = response.body().getID();
                        cate_ID = response.body().getCate_ID();
                        Intent intent = new Intent(getBaseContext(), ImageRecognitionActivity.class);
                        intent.putExtra("imageFile", imageFile);
                        intent.putExtra("category", category);
                        intent.putExtra("max_label", max_label);
                        intent.putExtra("ID", ID);
                        intent.putExtra("cate_ID", cate_ID);
                        frameLoading.setVisibility(View.GONE);
                        startActivity(intent);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<ResponeImageLabel> call, Throwable t) {
                    frameLoading.setVisibility(View.GONE);
                    Toast.makeText(SendImageActivity.this, "서버 연결 안됨", Toast.LENGTH_SHORT).show();
                    t.printStackTrace();
                }
            });
        }
    });
    findViewById(R.id.crop_image_menu_rotate_right).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            cropImageView.rotateImage(90);
        }
    });
    findViewById(R.id.btn_retry).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new ImageSelectionMethodDialog().show(getSupportFragmentManager(), "dialog");
        }
    });
    findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            frameLoading.setVisibility(View.VISIBLE);
            cropImageView.getCroppedImageAsync();
        }
    });
    frameLoading = (FrameLayout) findViewById(R.id.frame_loading);
}
Also used : Call(retrofit2.Call) Rect(android.graphics.Rect) Intent(android.content.Intent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageView(android.widget.ImageView) CropImageView(com.theartofdev.edmodo.cropper.CropImageView) View(android.view.View) Response(retrofit2.Response) Bitmap(android.graphics.Bitmap) Callback(retrofit2.Callback) FileOutputStream(java.io.FileOutputStream) CropImageView(com.theartofdev.edmodo.cropper.CropImageView) ImageSelectionMethodDialog(com.alphago.alphago.fragment.ImageSelectionMethodDialog)

Example 60 with Callback

use of zipkin2.Callback in project runelite by runelite.

the class FeedPanel method addItemToPanel.

private void addItemToPanel(FeedItem item) {
    JPanel avatarAndRight = new JPanel(new BorderLayout());
    avatarAndRight.setPreferredSize(new Dimension(0, 56));
    JLabel avatar = new JLabel();
    // width = 48+4 to compensate for the border
    avatar.setPreferredSize(new Dimension(52, 48));
    avatar.setBorder(new EmptyBorder(0, 4, 0, 0));
    switch(item.getType()) {
        case TWEET:
            try {
                Request request = new Request.Builder().url(item.getAvatar()).build();
                RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        log.warn(null, e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        try (ResponseBody responseBody = response.body()) {
                            if (!response.isSuccessful()) {
                                log.warn("Failed to download image " + item.getAvatar());
                                return;
                            }
                            BufferedImage icon;
                            synchronized (ImageIO.class) {
                                icon = ImageIO.read(responseBody.byteStream());
                            }
                            avatar.setIcon(new ImageIcon(icon));
                        }
                    }
                });
            } catch (IllegalArgumentException | NullPointerException e) {
                log.warn(null, e);
            }
            avatarAndRight.setBackground(TWEET_BACKGROUND);
            break;
        case OSRS_NEWS:
            if (OSRS_ICON != null) {
                avatar.setIcon(new ImageIcon(OSRS_ICON));
            }
            avatarAndRight.setBackground(OSRS_NEWS_BACKGROUND);
            break;
        default:
            if (RUNELITE_ICON != null) {
                avatar.setIcon(new ImageIcon(RUNELITE_ICON));
            }
            avatarAndRight.setBackground(BLOG_POST_BACKGROUND);
            break;
    }
    JPanel upAndContent = new JPanel();
    upAndContent.setLayout(new BoxLayout(upAndContent, BoxLayout.Y_AXIS));
    upAndContent.setBorder(new EmptyBorder(4, 8, 4, 4));
    upAndContent.setBackground(null);
    JPanel titleAndTime = new JPanel();
    titleAndTime.setLayout(new BorderLayout());
    titleAndTime.setBackground(null);
    Color darkerForeground = UIManager.getColor("Label.foreground").darker();
    JLabel titleLabel = new JLabel(item.getTitle());
    titleLabel.setFont(FontManager.getRunescapeSmallFont());
    titleLabel.setBackground(null);
    titleLabel.setForeground(darkerForeground);
    titleLabel.setPreferredSize(new Dimension(CONTENT_WIDTH - TIME_WIDTH, 0));
    Duration duration = Duration.between(Instant.ofEpochMilli(item.getTimestamp()), Instant.now());
    JLabel timeLabel = new JLabel(durationToString(duration));
    timeLabel.setFont(FontManager.getRunescapeSmallFont());
    timeLabel.setForeground(darkerForeground);
    titleAndTime.add(titleLabel, BorderLayout.WEST);
    titleAndTime.add(timeLabel, BorderLayout.EAST);
    JPanel content = new JPanel(new BorderLayout());
    content.setBackground(null);
    JLabel contentLabel = new JLabel(lineBreakText(item.getContent(), FontManager.getRunescapeSmallFont()));
    contentLabel.setBorder(new EmptyBorder(2, 0, 0, 0));
    contentLabel.setFont(FontManager.getRunescapeSmallFont());
    contentLabel.setForeground(darkerForeground);
    content.add(contentLabel, BorderLayout.CENTER);
    upAndContent.add(titleAndTime);
    upAndContent.add(content);
    upAndContent.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, Short.MAX_VALUE), new Dimension(0, Short.MAX_VALUE)));
    avatarAndRight.add(avatar, BorderLayout.WEST);
    avatarAndRight.add(upAndContent, BorderLayout.CENTER);
    Color backgroundColor = avatarAndRight.getBackground();
    Color hoverColor = backgroundColor.brighter().brighter();
    Color pressedColor = hoverColor.brighter();
    avatarAndRight.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent e) {
            avatarAndRight.setBackground(hoverColor);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            avatarAndRight.setBackground(backgroundColor);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            avatarAndRight.setBackground(pressedColor);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            avatarAndRight.setBackground(hoverColor);
            LinkBrowser.browse(item.getUrl());
        }
    });
    add(avatarAndRight);
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) BoxLayout(javax.swing.BoxLayout) BufferedImage(java.awt.image.BufferedImage) BorderLayout(java.awt.BorderLayout) EmptyBorder(javax.swing.border.EmptyBorder) Call(okhttp3.Call) MouseEvent(java.awt.event.MouseEvent) Color(java.awt.Color) Request(okhttp3.Request) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Duration(java.time.Duration) Box(javax.swing.Box) Dimension(java.awt.Dimension) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14