1
1
package fr .free .nrw .commons .profile ;
2
2
3
+ import android .app .AlertDialog ;
3
4
import android .content .Context ;
4
5
import android .content .Intent ;
6
+ import android .graphics .Bitmap ;
7
+ import android .net .Uri ;
5
8
import android .os .Bundle ;
9
+ import android .view .LayoutInflater ;
10
+ import android .view .Menu ;
11
+ import android .view .MenuInflater ;
12
+ import android .view .MenuItem ;
13
+ import android .view .View ;
14
+ import android .widget .ImageView ;
15
+ import android .widget .TextView ;
16
+ import androidx .appcompat .widget .Toolbar ;
17
+ import androidx .core .content .FileProvider ;
6
18
import androidx .fragment .app .Fragment ;
7
19
import androidx .fragment .app .FragmentManager ;
8
20
import androidx .viewpager .widget .ViewPager ;
9
21
import butterknife .BindView ;
10
22
import butterknife .ButterKnife ;
11
23
import com .google .android .material .tabs .TabLayout ;
12
24
import fr .free .nrw .commons .R ;
25
+ import fr .free .nrw .commons .Utils ;
13
26
import fr .free .nrw .commons .ViewPagerAdapter ;
27
+ import fr .free .nrw .commons .auth .SessionManager ;
14
28
import fr .free .nrw .commons .profile .achievements .AchievementsFragment ;
15
29
import fr .free .nrw .commons .profile .leaderboard .LeaderboardFragment ;
16
30
import fr .free .nrw .commons .theme .BaseActivity ;
31
+ import java .io .File ;
32
+ import java .io .FileOutputStream ;
33
+ import java .io .IOException ;
17
34
import java .util .ArrayList ;
18
35
import java .util .List ;
36
+ import javax .inject .Inject ;
19
37
20
38
/**
21
39
* This activity will set two tabs, achievements and
@@ -31,6 +49,12 @@ public class ProfileActivity extends BaseActivity {
31
49
@ BindView (R .id .tab_layout )
32
50
TabLayout tabLayout ;
33
51
52
+ @ BindView (R .id .toolbar )
53
+ Toolbar toolbar ;
54
+
55
+ @ Inject
56
+ SessionManager sessionManager ;
57
+
34
58
private ViewPagerAdapter viewPagerAdapter ;
35
59
private AchievementsFragment achievementsFragment ;
36
60
private LeaderboardFragment leaderboardFragment ;
@@ -40,7 +64,9 @@ protected void onCreate(Bundle savedInstanceState) {
40
64
super .onCreate (savedInstanceState );
41
65
setContentView (R .layout .activity_profile );
42
66
ButterKnife .bind (this );
43
- setTitle (R .string .Profile );
67
+ setSupportActionBar (toolbar );
68
+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
69
+ setTitle (sessionManager .getUserName ());
44
70
45
71
supportFragmentManager = getSupportFragmentManager ();
46
72
viewPagerAdapter = new ViewPagerAdapter (getSupportFragmentManager ());
@@ -49,6 +75,16 @@ protected void onCreate(Bundle savedInstanceState) {
49
75
setTabs ();
50
76
}
51
77
78
+ /**
79
+ * Navigate up event
80
+ * @return boolean
81
+ */
82
+ @ Override
83
+ public boolean onSupportNavigateUp () {
84
+ onBackPressed ();
85
+ return true ;
86
+ }
87
+
52
88
/**
53
89
* Creates a way to change current activity to AchievementActivity
54
90
* @param context
@@ -75,10 +111,84 @@ private void setTabs() {
75
111
viewPagerAdapter .notifyDataSetChanged ();
76
112
77
113
}
114
+
78
115
@ Override
79
116
public void onDestroy () {
80
117
super .onDestroy ();
81
118
compositeDisposable .clear ();
82
119
}
83
120
121
+ /**
122
+ * To inflate menu
123
+ * @param menu Menu
124
+ * @return boolean
125
+ */
126
+ @ Override
127
+ public boolean onCreateOptionsMenu (final Menu menu ) {
128
+ final MenuInflater menuInflater = getMenuInflater ();
129
+ menuInflater .inflate (R .menu .menu_about , menu );
130
+ return super .onCreateOptionsMenu (menu );
131
+ }
132
+
133
+ /**
134
+ * To receive the id of selected item and handle further logic for that selected item
135
+ * @param item MenuItem
136
+ * @return boolean
137
+ */
138
+ @ Override
139
+ public boolean onOptionsItemSelected (final MenuItem item ) {
140
+ // take screenshot in form of bitmap and show it in Alert Dialog
141
+ if (item .getItemId () == R .id .share_app_icon ) {
142
+ final View rootView = getWindow ().getDecorView ().findViewById (android .R .id .content );
143
+ final Bitmap screenShot = Utils .getScreenShot (rootView );
144
+ showAlert (screenShot );
145
+ return true ;
146
+ }
147
+ return super .onOptionsItemSelected (item );
148
+ }
149
+
150
+ /**
151
+ * It displays the alertDialog with Image of screenshot
152
+ * @param screenshot screenshot of the present screen
153
+ */
154
+ public void showAlert (final Bitmap screenshot ) {
155
+ final AlertDialog .Builder alert = new AlertDialog .Builder (this );
156
+ final LayoutInflater factory = LayoutInflater .from (this );
157
+ final View view = factory .inflate (R .layout .image_alert_layout , null );
158
+ final ImageView screenShotImage = view .findViewById (R .id .alert_image );
159
+ screenShotImage .setImageBitmap (screenshot );
160
+ final TextView shareMessage = view .findViewById (R .id .alert_text );
161
+ shareMessage .setText (R .string .achievements_share_message );
162
+ alert .setView (view );
163
+ alert .setPositiveButton (R .string .about_translate_proceed , (dialog , which ) -> shareScreen (screenshot ));
164
+ alert .setNegativeButton (android .R .string .cancel , (dialog , which ) -> dialog .cancel ());
165
+ alert .show ();
166
+ }
167
+
168
+ /**
169
+ * To take bitmap and store it temporary storage and share it
170
+ * @param bitmap bitmap of screenshot
171
+ */
172
+ void shareScreen (final Bitmap bitmap ) {
173
+ try {
174
+ final File file = new File (getExternalCacheDir (), "screen.png" );
175
+ final FileOutputStream fileOutputStream = new FileOutputStream (file );
176
+ bitmap .compress (Bitmap .CompressFormat .PNG , 100 , fileOutputStream );
177
+ fileOutputStream .flush ();
178
+ fileOutputStream .close ();
179
+ file .setReadable (true , false );
180
+
181
+ final Uri fileUri = FileProvider
182
+ .getUriForFile (getApplicationContext (),
183
+ getPackageName () + ".provider" , file );
184
+ grantUriPermission (getPackageName (), fileUri , Intent .FLAG_GRANT_READ_URI_PERMISSION );
185
+ final Intent intent = new Intent (android .content .Intent .ACTION_SEND );
186
+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
187
+ intent .putExtra (Intent .EXTRA_STREAM , fileUri );
188
+ intent .setType ("image/png" );
189
+ startActivity (Intent .createChooser (intent , getString (R .string .share_image_via )));
190
+ } catch (final IOException e ) {
191
+ e .printStackTrace ();
192
+ }
193
+ }
84
194
}
0 commit comments